公司团队项目、产品已经完全抛弃EF,SqlSugar定位不是ORM,而是为了方便的让你去写Sql。
SqlSugar 媲美原生ADO.NET的性能,语法简洁,并且支持 Json 、Dynamic、 List<T>、 List<string[]>、 ValueType和 Dictionary 等多种类型的返回值,有很多亮点。
SqlSugar 功能介绍:
查询/Search
1、Queryable 用于拉姆达表达式操作(不久将支持Join多表) / Select single table or view
//---------Queryable, 扩展函数查询 (针对单表或者视图查询)-----//
//查询条数var count = db.Queryable().Where(c => c.id > 10).Count();
//查询第一条var first = db.Queryable().Where(c => c.id == 1).First();var first2 = db.Queryable ().Where(c => c.id == 1).FirstOrDefault();//取10-20条var page1 = db.Queryable ().Where(c => c.id > 10).OrderBy(it => it.id).Skip(10).Take(20).ToList();//上一句的简化写法,同样取10-20条var page2 = db.Queryable ().Where(c => c.id > 10).OrderBy(it => it.id).ToPageList(2, 10);//从第2条开始以后取所有var skip = db.Queryable ().Where(c => c.id > 10).OrderBy(it => it.id).Skip(2).ToList();
//查询所有var student = db.Queryable().ToList();var studentDynamic = db.Queryable ().ToDynamic();var studentJson = db.Queryable ().ToJson();//查询单条var single = db.Queryable ().Single(c => c.id == 1);//查询单条没有记录返回空对象var single2 = db.Queryable ().Where(c => c.id == 1).SingleOrDefault();
//取前2条var take = db.Queryable().Where(c => c.id > 10).OrderBy(it => it.id).Take(2).ToList();// Not like string conval = "a";var notLike = db.Queryable ().Where(c => !c.name.Contains(conval.ToString())).ToList();//Likeconval = "三";var like = db.Queryable ().Where(c => c.name.Contains(conval)).ToList();// 可以在拉姆达使用 ToString和 Convert,比EF出色的地方var convert1 = db.Queryable ().Where(c => c.name == "a".ToString()).ToList();var convert2 = db.Queryable ().Where(c => c.id == Convert.ToInt32("1")).ToList();// var convert3 = db.Queryable ().Where(c => DateTime.Now > Convert.ToDateTime("2015-1-1")).ToList();var convert4 = db.Queryable ().Where(c => DateTime.Now > DateTime.Now).ToList();//支持字符串Where 让你解决,更复杂的查询var student12 = db.Queryable ().Where(c => "a" == "a").Where("id>100").ToList();var student13 = db.Queryable ().Where(c => "a" == "a").Where("id>100 and id in( select 1)").ToList();//存在记录反回true,则否返回falsebool isAny100 = db.Queryable ().Any(c => c.id == 100);bool isAny1 = db.Queryable ().Any(c => c.id == 1);//获取最大Idobject maxId = db.Queryable ().Max(it => it.id);int maxId1 = db.Queryable ().Max(it => it.id).ObjToInt();//拉姆达int maxId2 = db.Queryable ().Max ("id"); //字符串写法//获取最小int minId1 = db.Queryable ().Where(c => c.id > 0).Min(it => it.id).ObjToInt();//拉姆达int minId2 = db.Queryable ().Where(c => c.id > 0).Min ("id");//字符串写法//order By var orderList = db.Queryable ().OrderBy("id desc,name asc").ToList();//字符串支持多个排序//可以多个order by表达示var order2List = db.Queryable ().OrderBy(it=>it.name).OrderBy(it => it.id, OrderByType.desc).ToList(); // order by name as ,order by id desc//Invar intArray=new []{ "5", "2", "3"};var intList = intArray.ToList();var list0 = db.Queryable ().In(it => it.id, 1,2,3).ToList();var list1 = db.Queryable ().In(it=>it.id, intArray).ToList();var list2 = db.Queryable ().In("id", intArray).ToList();var list3 = db.Queryable ().In(it => it.id, intList).ToList();var list4 = db.Queryable ().In("id", intList).ToList();//分组查询var list7 = db.Queryable ().Where(c => c.id < 20).GroupBy(it => it.sex).Select("sex,Count=count(*)").ToDynamic();var list8 = db.Queryable ().Where(c => c.id < 20).GroupBy(it => it.sex).GroupBy(it=>it.id).Select("id,sex,Count=count(*)").ToDynamic();List list9 = db.Queryable ().Where(c => c.id < 20).GroupBy(it => it.sex).Select ("Sex,Count=count(*)").ToList();List list10 = db.Queryable ().Where(c => c.id < 20).GroupBy("sex").Select ("Sex,Count=count(*)").ToList();//SELECT Sex,Count=count(*) FROM Student WHERE 1=1 AND (id < 20) GROUP BY Sex --生成结果
2、SqlQuery 用于执行原生SQL和存储过程
//转成listListlist1 = db.SqlQuery ("select * from Student");//转成list带参List list2 = db.SqlQuery ("select * from Student where id=@id", new { id = 1 });//转成dynamicdynamic list3 = db.SqlQueryDynamic("select * from student");//转成jsonstring list4 = db.SqlQueryJson("select * from student");//返回intvar list5 = db.SqlQuery ("select top 1 id from Student").SingleOrDefault();//反回键值Dictionary list6 = db.SqlQuery >("select id,name from Student").ToDictionary(it => it.Key, it => it.Value);//反回List var list7 = db.SqlQuery ("select top 1 id,name from Student").SingleOrDefault();//存储过程var spResult = db.SqlQuery ("exec sp_school @p1,@p2", new { p1 = 1, p2 = 2 });//获取第一行第一列的值string v1 = db.GetString("select '张三' as name");int v2 = db.GetInt("select 1 as name");double v3 = db.GetDouble("select 1 as name");decimal v4 = db.GetDecimal("select 1 as name");
3、Sqlable 更贴近SQL的一种编程方式,适用于复杂查询
//---------Sqlable,创建多表查询---------////多表查询ListdataList = db.Sqlable().From("school", "s").Join("student", "st", "st.id", "s.id", JoinType.INNER).Join("student", "st2", "st2.id", "st.id", JoinType.LEFT).Where("s.id>100 and s.id<@id").Where("1=1")//可以多个WHERE.SelectToList ("st.*", new { id = 1 });//多表分页List dataPageList = db.Sqlable().From("school", "s").Join("student", "st", "st.id", "s.id", JoinType.INNER).Join("student", "st2", "st2.id", "st.id", JoinType.LEFT).Where("s.id>100 and s.id<100").SelectToPageList ("st.*", "s.id", 1, 10);//多表分页WHERE加子查询List dataPageList2 = db.Sqlable().From("school", "s").Join("student", "st", "st.id", "s.id", JoinType.INNER).Join("student", "st2", "st2.id", "st.id", JoinType.LEFT).Where("s.id>100 and s.id<100 and s.id in (select 1 )" /*这里面写子查询都可以*/).SelectToPageList ("st.*", "s.id", 1, 10); //--------转成List Dynmaic 或者 Json-----////不分页var list1 = db.Sqlable().From("student", "s").Join("school", "l", "s.sch_id", "l.id and l.id=@id", JoinType.INNER).SelectToDynamic("*", new { id = 1 });var list2 = db.Sqlable().From("student", "s").Join("school", "l", "s.sch_id", "l.id and l.id=@id", JoinType.INNER).SelectToJson("*", new { id = 1 });var list3 = db.Sqlable().From("student", "s").Join("school", "l", "s.sch_id", "l.id and l.id=@id", JoinType.INNER).SelectToDataTable("*", new { id = 1 });//分页var list4 = db.Sqlable().From("student", "s").Join("school", "l", "s.sch_id", "l.id and l.id=@id", JoinType.INNER).SelectToPageDynamic("s.*", "l.id", 1, 10, new { id = 1 });var list5 = db.Sqlable().From("student", "s").Join("school", "l", "s.sch_id", "l.id and l.id=@id", JoinType.INNER).SelectToPageTable("s.*", "l.id", 1, 10, new { id = 1 });var list6 = db.Sqlable().From("student", "s").Join("school", "l", "s.sch_id", "l.id and l.id=@id", JoinType.INNER).SelectToPageDynamic("s.*", "l.id", 1, 10, new { id = 1 });//--------拼接-----//Sqlable sable = db.Sqlable().From ("s").Join ("l", "s.sch_id", "l.id", JoinType.INNER);string name = "a";int id = 1;if (!string.IsNullOrEmpty(name)){sable = sable.Where("s.name=@name");}if (!string.IsNullOrEmpty(name)){sable = sable.Where("s.id=@id or s.id=100");}if (id > 0){sable = sable.Where("l.id in (select top 10 id from school)");//where加子查询}var pars = new { id = id, name = name };int pageCount = sable.Count(pars);var list7 = sable.SelectToPageList ("s.*","l.id desc",1,20, pars);
4、新容器转换
using (SqlSugarClient db = SugarDao.GetInstance()){var list2 = db.Queryable().Where(c => c.id < 10).Select(c => new classNew { newid = c.id, newname = c.name, xx_name = c.name }).ToList();//不支持匿名类转换,也不建议使用var list3 = db.Queryable ().Where(c => c.id < 10).Select(c => new { newid = c.id, newname = c.name, xx_name = c.name }).ToDynamic();//匿名类转换var list4 = db.Queryable ().Where(c => c.id < 10).Select("id as newid, name as newname ,name as xx_name").ToDynamic();//匿名类转换
添加/Add
using (SqlSugarClient db = SugarDao.GetInstance())//开启数据库连接{Student s = new Student(){name = "张" + new Random().Next(1, int.MaxValue)};db.Insert(s); //插入一条记录 (有主键也好,没主键也好,有自增列也好都可以插进去)Listlist = new List (){new Student(){name="张"+new Random().Next(1,int.MaxValue)},new Student(){name="张"+new Random().Next(1,int.MaxValue)}};db.InsertRange(list); //批量插入}}
更新/Update
using (var db = SugarDao.GetInstance()){//指定列更新db.Update(new { name = "蓝翔2" }, it => it.id == id);db.Update (new { name = "蓝翔2" }, 1, 3, 12);db.Update (new { name = "蓝翔2" }, new string[] { "1", "2" });//整个实体更新db.Update(new School { id = id, name = "蓝翔2" });db.Update (new School { id = id, name = "蓝翔2" }, it => it.id == id);//设置不更新列db.DisableUpdateColumns = new string[] { "CreateTime" };//设置CreateTime不更新TestUpdateColumns updObj = new TestUpdateColumns(){VGUID = Guid.Parse("542b5a27-6984-47c7-a8ee-359e483c8470"),Name = "xx",Name2 = "xx2",IdentityField = 0,CreateTime = null};//CreateTime将不会被更新db.Update(updObj);//以前实现这种更新需要用指定列的方式实现,现在就简单多了。}
删除和假删除
using (var db = SugarDao.GetInstance()){//真删除db.Delete(10);//注意主键必需为实体类的第一个属性db.Delete (it => it.id > 100);db.Delete (new string[] { "100", "101", "102" });//假删除//db.FalseDelete ("is_del", 100);//等同于 update school set is_del=0 where id in(100)//db.FalseDelete ("is_del", it=>it.id==100);}
还有更多的功能介绍没有复制过来……