SqlSugar源码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
sunkaixuan 83e60ad446 - 8 years ago
OrmTest - 8 years ago
SqlSugar - 8 years ago
.gitignore - 8 years ago
README.md Update README.md 8 years ago
SqlSugar.sln Ok 8 years ago

README.md

SqlSugar 4.X API

3.X https://github.com/sunkaixuan/SqlSugar/tree/master

1. Query

1.1 Create Connection

If you have system table permissions, use SystemTableConfig,else use AttributeConfig

     SqlSugarClient db = new SqlSugarClient(new SystemTableConfig() 
     { ConnectionString = Config.ConnectionString, DbType =DbType.SqlServer, IsAutoCloseConnection = true });

SystemTableConfig: https://github.com/sunkaixuan/SqlSugar/wiki/SystemTableConfig

AttrbuteConfig https://github.com/sunkaixuan/SqlSugar/wiki/AttributeCofnig

1.2 Introduction

  var getAll = db.Queryable<Student>().ToList();
  var getAllNoLock = db.Queryable<Student>().With(SqlWith.NoLock).ToList();
  var getByPrimaryKey = db.Queryable<Student>().InSingle(2);
  var getByWhere = db.Queryable<Student>().Where(it => it.Id == 1 || it.Name == "a").ToList();
  var getByFuns = db.Queryable<Student>().Where(it => NBORM.IsNullOrEmpty(it.Name)).ToList();

1.3 Page

var pageIndex = 1;
var pageSize = 2;
var totalCount = 0;
//page
var page = db.Queryable<Student>().ToPageList(pageIndex, pageSize, ref totalCount);

//page join
var pageJoin = db.Queryable<Student, School>((st, sc) => new object[] {
JoinType.Left,st.SchoolId==sc.Id
}).ToPageList(pageIndex, pageSize, ref totalCount);

//top 5
var top5 = db.Queryable<Student>().Take(5).ToList();

//skip5
var skip5 = db.Queryable<Student>().Skip(5).ToList();

1.4 Join

//join  2
var list = db.Queryable<Student, School>((st, sc) => new object[] {
JoinType.Left,st.SchoolId==sc.Id
}).ToList();

//join  3
var list2 = db.Queryable<Student, School,Student>((st, sc,st2) => new object[] {
JoinType.Left,st.SchoolId==sc.Id,
JoinType.Left,st.SchoolId==st2.Id
}).ToList();

//join return List<ViewModelStudent>
var list3 = db.Queryable<Student, School>((st, sc) => new object[] {
JoinType.Left,st.SchoolId==sc.Id
}).Select<Student,School,ViewModelStudent>((st,sc)=>new ViewModelStudent { Name= st.Name,SchoolId=sc.Id }).ToList();

//join Order By (order by st.id desc,sc.id desc)
var list4 = db.Queryable<Student, School>((st, sc) => new object[] {
JoinType.Left,st.SchoolId==sc.Id
})
.OrderBy(st=>st.Id,OrderByType.Desc)
.OrderBy<School>(sc=>sc.Id,OrderByType.Desc)
.Select<Student, School, ViewModelStudent>((st, sc) => new ViewModelStudent { Name = st.Name, SchoolId = sc.Id }).ToList();

1.5 SqlFunctions

var t1 = db.Queryable<Student>().Where(it => NBORM.ToLower(it.Name) == NBORM.ToLower("JACK")).ToList();
//SELECT [Id],[SchoolId],[Name],[CreateTime] FROM [Student]  WHERE ((LOWER([Name])) = (LOWER(@MethodConst0)) )

/***More Functions***/
//NBORM.IsNullOrEmpty(object thisValue)
//NBORM.ToLower(object thisValue) 
//NBORM.string ToUpper(object thisValue) 
//NBORM.string Trim(object thisValue) 
//NBORM.bool Contains(string thisValue, string parameterValue) 
//NBORM.ContainsArray(object[] thisValue, string parameterValue) 
//NBORM.StartsWith(object thisValue, string parameterValue) 
//NBORM.EndsWith(object thisValue, string parameterValue)
//NBORM.Equals(object thisValue, object parameterValue) 
//NBORM.DateIsSame(DateTime date1, DateTime date2)
//NBORM.DateIsSame(DateTime date1, DateTime date2, DateType dataType) 
//NBORM.DateAdd(DateTime date, int addValue, DateType millisecond) 
//NBORM.DateAdd(DateTime date, int addValue) 
//NBORM.DateValue(DateTime date, DateType dataType) 
//NBORM.Between(object value, object start, object end) 
//NBORM.ToInt32(object value) 
//NBORM.ToInt64(object value)
//NBORM.ToDate(object value) 
//NBORM.ToString(object value) 
//NBORM.ToDecimal(object value) 
//NBORM.ToGuid(object value) 
//NBORM.ToDouble(object value) 
//NBORM.ToBool(object value) 
//NBORM.Substring(object value, int index, int length)
//NBORM.Replace(object value, string oldChar, string newChar)
//NBORM.Length(object value) { throw new NotImplementedException(); }
//NBORM.AggregateSum(object thisValue) 
//NBORM.AggregateAvg<TResult>(TResult thisValue)
//NBORM.AggregateMin(object thisValue) 
//NBORM.AggregateMax(object thisValue) 
//NBORM.AggregateCount(object thisValue) 

1.6 Select

var s1 = db.Queryable<Student>().Select(it => new ViewModelStudent2 { Name = it.Name, Student = it }).ToList();
var s2 = db.Queryable<Student>().Select(it => new { id = it.Id, w = new { x = it } }).ToList();
var s3 = db.Queryable<Student>().Select(it => new { newid = it.Id }).ToList();
var s4 = db.Queryable<Student>().Select(it => new { newid = it.Id, obj = it }).ToList();
var s5 = db.Queryable<Student>().Select(it => new ViewModelStudent2 { Student = it, Name = it.Name }).ToList();

1.7 Join Sql

var join3 = db.Queryable("Student", "st")
                .AddJoinInfo("School", "sh", "sh.id=st.schoolid")
                .Where("st.id>@id")
                .AddParameters(new { id = 1 })
                .Select("st.*").ToList();
 //SELECT st.* FROM [Student] st Left JOIN School sh ON sh.id=st.schoolid   WHERE st.id>@id 

1.8 ADO.NET

var db = GetInstance();
var t1= db.Ado.SqlQuery<string>("select 'a'");
var t2 = db.Ado.GetInt("select 1");
var t3 = db.Ado.GetDataTable("select 1 as id");
//more
//db.Ado.GetXXX...

2. Insert

var insertObj = new Student() { Name = "jack", CreateTime = Convert.ToDateTime("2010-1-1") ,SchoolId=1};

//Insert reutrn Insert Count
var t2 = db.Insertable(insertObj).ExecuteCommand();

//Insert reutrn Identity Value
var t3 = db.Insertable(insertObj).ExecuteReutrnIdentity();


//Only  insert  Name 
var t4 = db.Insertable(insertObj).InsertColumns(it => new { it.Name,it.SchoolId }).ExecuteReutrnIdentity();


//Ignore TestId
var t5 = db.Insertable(insertObj).IgnoreColumns(it => new { it.Name, it.TestId }).ExecuteReutrnIdentity();


//Ignore   TestId
var t6 = db.Insertable(insertObj).IgnoreColumns(it => it == "Name" || it == "TestId").ExecuteReutrnIdentity();


//Use Lock
var t8 = db.Insertable(insertObj).With(SqlWith.UpdLock).ExecuteCommand();


var insertObj2 = new Student() { Name = null, CreateTime = Convert.ToDateTime("2010-1-1") };
var t9 = db.Insertable(insertObj2).Where(true/* Is insert null */, true/*off identity*/).ExecuteCommand();

//Insert List<T>
var insertObjs = new List<Student>();
for (int i = 0; i < 1000; i++)
{
insertObjs.Add(new Student() { Name = "name" + i });
}
var s9 = db.Insertable(insertObjs.ToArray()).InsertColumns(it => new { it.Name }).ExecuteCommand();

3. Delete

var t1 = db.Deleteable<Student>().Where(new Student() { Id = 1 }).ExecuteCommand();

//use lock
var t2 = db.Deleteable<Student>().With(SqlWith.RowLock).ExecuteCommand();


//by primary key
var t3 = db.Deleteable<Student>().In(1).ExecuteCommand();

//by primary key array
var t4 = db.Deleteable<Student>().In(new int[] { 1, 2 }).ExecuteCommand();

//by expression
var t5 = db.Deleteable<Student>().Where(it => it.Id == 1).ExecuteCommand();

4. Update

var db = GetInstance();
var updateObj = new Student() { Id = 1, Name = "jack", SchoolId = 0, CreateTime = Convert.ToDateTime("2017-05-21 09:56:12.610") };
var updateObjs = new List<Student>() { updateObj, new Student() { Id = 2, Name = "sun", SchoolId = 0 } }.ToArray();
db.IgnoreColumns.Add("TestId", "Student");
//db.MappingColumns.Add("id","dbid", "Student");


//update reutrn Update Count
var t1= db.Updateable(updateObj).ExecuteCommand();

//Only  update  Name 
var t3 = db.Updateable(updateObj).UpdateColumns(it => new { it.Name }).ExecuteCommand();


//Ignore  Name and TestId
var t4 = db.Updateable(updateObj).IgnoreColumns(it => new { it.Name, it.TestId }).ExecuteCommand();

//Ignore  Name and TestId
var t5 = db.Updateable(updateObj).IgnoreColumns(it => it == "Name" || it == "TestId").With(SqlWith.UpdLock).ExecuteCommand();


//Use Lock
var t6 = db.Updateable(updateObj).With(SqlWith.UpdLock).ExecuteCommand();

//update List<T>
var t7 = db.Updateable(updateObjs).ExecuteCommand();

//Re Set Value
var t8 = db.Updateable(updateObj)
.ReSetValue(it => it.Name == (it.Name + 1)).ExecuteCommand();

//Where By Expression
var t9 = db.Updateable(updateObj).Where(it => it.Id == 1).ExecuteCommand();