經過四天的努力終於將SqlSugar ORM 成功支持ORACLE資料庫 優點: 1、高性能,達到原生最高水準,比SqlHelper性能要高,比Dapper快30% 比EF快50% 2、支持多種資料庫 ,sql版本更新最快,其它會定期更新,可以在多種資料庫用一種編程方式 3、支持.net Core ...
經過四天的努力終於將SqlSugar ORM 成功支持ORACLE資料庫
優點:
1、高性能,達到原生最高水準,比SqlHelper性能要高,比Dapper快30% 比EF快50%
2、支持多種資料庫 ,sql版本更新最快,其它會定期更新,可以在多種資料庫用一種編程方式
3、支持.net Core
4、支持多庫並行計算
5、帶有實體生成函數和T4模版生成實體
6、無需任何配置 ,還你一個乾凈的model
2、功能介紹/Product function introduction
查詢/Search
1、Queryable用於拉姆達操作資料庫
2、SqlQuery用於執行SQL和存儲過程
3、Sqlable一種更新近SQL的寫法
1、Queryable 用於拉姆達表達式操作(支持Join多表) / Select single table or view
//查詢所有 var student = db.Queryable<STUDENT>().ToList(); var studentDynamic = db.Queryable<STUDENT>().ToDynamic(); var studentJson = db.Queryable<STUDENT>().ToJson(); //查詢單條 var single = db.Queryable<STUDENT>().Single(c => c.ID == 1); //查詢單條沒有記錄返回空對象 var singleOrDefault = db.Queryable<STUDENT>().SingleOrDefault(c => c.ID == 11111111); //查詢單條沒有記錄返回空對象 var single2 = db.Queryable<STUDENT>().Where(c => c.ID == 1).SingleOrDefault(); //查詢第一條 var first = db.Queryable<STUDENT>().Where(c => c.ID == 1).First(); var first2 = db.Queryable<STUDENT>().Where(c => c.ID == 1).FirstOrDefault(); //取10-20條 var page1 = db.Queryable<STUDENT>().Where(c => c.ID > 10).OrderBy(it => it.ID).Skip(10).Take(20).ToList(); //上一句的簡化寫法,同樣取10-20條 var page2 = db.Queryable<STUDENT>().Where(c => c.ID > 10).OrderBy(it => it.ID).ToPageList(2, 10); //查詢條數 var count = db.Queryable<STUDENT>().Where(c => c.ID > 10).Count(); //從第2條開始以後取所有 var skip = db.Queryable<STUDENT>().Where(c => c.ID > 10).OrderBy(it => it.ID).Skip(2).ToList(); //取前2條 var take = db.Queryable<STUDENT>().Where(c => c.ID > 10).OrderBy(it => it.ID).Take(2).ToList(); // Not like string conval = "a"; var notLike = db.Queryable<STUDENT>().Where(c => !c.NAME.Contains(conval.ToString())).ToList(); //Like conval = "三"; var like = db.Queryable<STUDENT>().Where(c => c.NAME.Contains(conval)).ToList(); //支持字元串Where 讓你解決,更複雜的查詢 var student12 = db.Queryable<STUDENT>().Where(c => "a" == "a").Where("id>:id", new { id = 1 }).ToList(); var student13 = db.Queryable<STUDENT>().Where(c => "a" == "a").Where("id>100 ").ToList(); //存在記錄反回true,則否返回false bool isAny100 = db.Queryable<STUDENT>().Any(c => c.ID == 100); bool isAny1 = db.Queryable<STUDENT>().Any(c => c.ID == 1); //獲取最大Id object maxId = db.Queryable<STUDENT>().Max(it => it.ID); int maxId1 = db.Queryable<STUDENT>().Max(it => it.ID).ObjToInt();//拉姆達 int maxId2 = db.Queryable<STUDENT>().Max<STUDENT, int>("id"); //字元串寫法 //獲取最小 int minId1 = db.Queryable<STUDENT>().Where(c => c.ID > 0).Min(it => it.ID).ObjToInt();//拉姆達 int minId2 = db.Queryable<STUDENT>().Where(c => c.ID > 0).Min<STUDENT, int>("id");//字元串寫法 //order By var orderList = db.Queryable<STUDENT>().OrderBy("id desc,name asc").ToList();//字元串支持多個排序 //可以多個order by表達示 var order2List = db.Queryable<STUDENT>().OrderBy(it => it.NAME).OrderBy(it => it.ID, OrderByType.desc).ToList(); // order by name as ,order by id desc //In var intArray = new[] { "5", "2", "3" }; var intList = intArray.ToList(); var list0 = db.Queryable<STUDENT>().In(it => it.ID, 1, 2, 3).ToList(); var list1 = db.Queryable<STUDENT>().In(it => it.ID, intArray).ToList(); var list2 = db.Queryable<STUDENT>().In("id", intArray).ToList(); var list3 = db.Queryable<STUDENT>().In(it => it.ID, intList).ToList(); var list4 = db.Queryable<STUDENT>().In("id", intList).ToList(); //分組查詢 var list7 = db.Queryable<STUDENT>().Where(c => c.ID < 20).GroupBy(it => it.SEX).Select("sex,count(*) count").ToDynamic(); var list8 = db.Queryable<STUDENT>().Where(c => c.ID < 20).GroupBy(it => it.SEX).GroupBy(it => it.ID).Select("id,sex,count(*) count").ToDynamic(); List<SexTotal> list9 = db.Queryable<STUDENT>().Where(c => c.ID < 20).GroupBy(it => it.SEX).Select<STUDENT, SexTotal>("SEX,count(*) COUNT").ToList(); List<SexTotal> list10 = db.Queryable<STUDENT>().Where(c => c.ID < 20).GroupBy("sex").Select<STUDENT, SexTotal>("SEX,count(*) COUNT").ToList(); //SELECT Sex,Count=count(*) FROM Student WHERE 1=1 AND (id < 20) GROUP BY Sex --生成結果 //2表關聯查詢 var jList = db.Queryable<STUDENT>() .JoinTable<STUDENT, SCHOOL>((s1, s2) => s1.SCH_ID == s2.ID) //預設left join .Where<STUDENT, SCHOOL>((s1, s2) => s1.ID == 1) .Select("s1.*,s2.name as schName") .ToDynamic(); /*等於同於 SELECT s1.*,s2.name as schName FROM [Student] s1 LEFT JOIN [School] s2 ON s1.sch_id = s2.id WHERE s1.id = 1 */ //2表關聯查詢並分頁 var jList2 = db.Queryable<STUDENT>() .JoinTable<STUDENT, SCHOOL>((s1, s2) => s1.SCH_ID == s2.ID) //預設left join //如果要用inner join這麼寫 //.JoinTable<Student, School>((s1, s2) => s1.sch_id == s2.id ,JoinType.INNER) .Where<STUDENT, SCHOOL>((s1, s2) => s1.ID > 1) .OrderBy<STUDENT, SCHOOL>((s1, s2) => s1.NAME) .Skip(10) .Take(20) .Select("s1.*,s2.name as schName") .ToDynamic(); //3表查詢並分頁 var jList3 = db.Queryable<STUDENT>() .JoinTable<STUDENT, SCHOOL>((s1, s2) => s1.SCH_ID == s2.ID) // left join School s2 on s1.id=s2.id .JoinTable<STUDENT, SCHOOL>((s1, s3) => s1.SCH_ID == s3.ID) // left join School s3 on s1.id=s3.id .Where<STUDENT, SCHOOL>((s1, s2) => s1.ID > 1) // where s1.id>1 .Where<STUDENT>(s1 => s1.ID > 0) .OrderBy<STUDENT, SCHOOL>((s1, s2) => s1.ID) //order by s1.id 多個order可以 .oderBy().orderby 疊加 .Skip(10) .Take(20) .Select("s1.*,s2.name as schName,s3.name as schName2")//select目前只支持這種寫法 .ToDynamic(); //上面的方式都是與第一張表join,第三張表想與第二張表join寫法如下 List<classNew> jList4 = db.Queryable<STUDENT>() .JoinTable<STUDENT, SCHOOL>((s1, s2) => s1.SCH_ID == s2.ID) // left join School s2 on s1.id=s2.id .JoinTable<STUDENT, SCHOOL, AREA>((s1, s2, a1) => a1.ID == s2.AREAID)// left join Area a1 on a1.id=s2.AreaId .Select<STUDENT, SCHOOL, AREA, classNew>((s1, s2, a1) => new classNew { NEWID = s1.ID, STUDENTNAME = s1.NAME, SCHOOLNAME = s2.NAME, AREANAME = a1.NAME }).ToList(); //最多支持5表查詢,太過複雜的建議用Sqlable或者SqlQuery,我們的Queryable只適合輕量級的查詢 //拼接 var queryable = db.Queryable<STUDENT>().Where(it => true); if (maxId.ObjToInt() == 1) { queryable.Where(it => it.ID == 1); } else { queryable.Where(it => it.ID == 2); } var listJoin = queryable.ToList(); //queryable和SqlSugarClient解耦 var par = new Queryable<STUDENT>().Where(it => it.ID == 1);//聲名沒有connection對象的Queryable par.DB = db; var listPar = par.ToList(); //查看生成的sql和參數 var id=1; var sqlAndPars = db.Queryable<STUDENT>().Where(it => it.ID == id).OrderBy(it => it.ID).ToSql(); //函數的支持(欄位暫不支持函數,只有參數支持) 目前只支持這麼多 var par1 = "2015-1-1"; var par2 = " 我 有空格, "; var r1 = db.Queryable<STUDENT>().Where(it => it.NAME == par1.ObjToString()).ToSql(); //ObjToString會將null轉轉成"" var r2 = db.Queryable<INSERTTEST>().Where(it => it.D1 == par1.ObjToDate()).ToSql(); var r3 = db.Queryable<INSERTTEST>().Where(it => it.ID == 1.ObjToInt()).ToSql();//ObjToInt會將null轉轉成0 var r4 = db.Queryable<INSERTTEST>().Where(it => it.ID == 2.ObjToDecimal()).ToSql(); var r5 = db.Queryable<INSERTTEST>().Where(it => it.ID == 3.ObjToMoney()).ToSql(); var r6 = db.Queryable<INSERTTEST>().Where(it => it.V1 == par2.Trim()).ToSql(); var convert1 = db.Queryable<STUDENT>().Where(c => c.NAME == "a".ToString()).ToList(); var convert2 = db.Queryable<STUDENT>().Where(c => c.ID == Convert.ToInt32("1")).ToList();// var convert3 = db.Queryable<STUDENT>().Where(c => DateTime.Now > Convert.ToDateTime("2015-1-1")).ToList(); var convert4 = db.Queryable<STUDENT>().Where(c => DateTime.Now > DateTime.Now).ToList(); var c1 = db.Queryable<STUDENT>().Where(c =>c.NAME.Contains("a")).ToList(); var c2 = db.Queryable<STUDENT>().Where(c => c.NAME.StartsWith("a")).ToList(); var c3 = db.Queryable<STUDENT>().Where(c => c.NAME.EndsWith("a")).ToList();
2、SqlQuery 用於執行原生SQL和存儲過程 / Execution of native SQL
using (var db = SugarDao.GetInstance()) { //轉成list List<STUDENT> list1 = db.SqlQuery<STUDENT>("select * from Student"); //轉成list帶參 List<STUDENT> list2 = db.SqlQuery<STUDENT>("select * from Student where id=:id", new { id = 1 }); //轉成dynamic dynamic list3 = db.SqlQueryDynamic("select * from student"); //轉成json string list4 = db.SqlQueryJson("select * from student"); //返回int var list5 = db.SqlQuery<int>("select id from Student where rownum=1").SingleOrDefault(); //反回鍵值 Dictionary<string, string> list6 = db.SqlQuery<KeyValuePair<string, string>>("select id,name from Student").ToDictionary(it => it.Key, it => it.Value); //反回List<string[]> var list7 = db.SqlQuery<string[]>("select id,name from Student where rownum=1").SingleOrDefault(); //存儲過程 //var spResult = db.SqlQuery<SCHOOL>(@"begin exec sp_school (:p1,:p2); end;", new { p1 = 1, p2 = 2 }); //獲取第一行第一列的值 string v1 = db.GetString("select '張三' as name from dual"); int v2 = db.GetInt("select 1 as name from dual"); double v3 = db.GetDouble("select 1 as name from dual"); decimal v4 = db.GetDecimal("select 1 as name from dual"); //.... }
3、Sqlable 更貼近SQL的一種編程方式,適用於複雜查詢 / Complex query
//多表查詢 List<SCHOOL> dataList = 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 .OrderBy("s.id") .SelectToList<SCHOOL/*新的Model我這裡沒有所以寫的School*/>("st.*", new { id = 1 }); //多表分頁 List<SCHOOL> 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<SCHOOL>("st.*", "s.id", 1, 10); //多表分頁WHERE加子查詢 List<SCHOOL> 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 as id from dual )" /*這裡面寫子查詢都可以*/) .SelectToPageList<SCHOOL>("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<STUDENT>("s").Join<SCHOOL>("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 id from school where rownum<10)");//where加子查詢 } var pars = new { id = id, name = name }; int pageCount = sable.Count(pars); var list7 = sable.SelectToPageList<STUDENT>("s.*", "l.id desc", 1, 20, pars);View Code
4、新容器轉換 / To new container
using (SqlSugarClient db = SugarDao.GetInstance()) { var list2 = db.Queryable<STUDENT>().Where(c => c.ID < 10).Select(c => new classNew { NEWID = c.ID, NEWNAME = c.NAME, XX_NAME = c.NAME }).ToList();//不支持匿名類轉換,也不建議使用 var list3 = db.Queryable<STUDENT>().Where(c => c.ID < 10).Select(c => new { newid = c.ID, newname = c.NAME, xx_name = c.NAME }).ToDynamic();//匿名類轉換 var list4 = db.Queryable<STUDENT>().Where(c => c.ID < 10).Select("id as newid, name as newname ,name as xx_name").ToDynamic();//匿名類轉換 var jList1 = db.Queryable<STUDENT>() .JoinTable<STUDENT, SCHOOL>((s1, s2) => s1.SCH_ID == s2.ID) // left join School s2 on s1.id=s2.id .Where<STUDENT, SCHOOL>((s1, s2) => s1.ID > 1) // where s1.id>1 .OrderBy<STUDENT, SCHOOL>((s1, s2) => s1.ID) //order by s1.id 多個order可以 .oderBy().orderby 疊加 .Skip(1) .Take(2) .Select<STUDENT, SCHOOL, classNew>((s1, s2) => new classNew() { NEWID = s1.ID, NEWNAME = s2.NAME, XX_NAME = s1.NAME })//select目前只支持這種寫法 .ToList(); var jList2 = db.Queryable<STUDENT>() .JoinTable<STUDENT, SCHOOL>((s1, s2) => s1.SCH_ID == s2.ID) // left join School s2 on s1.id=s2.id .Where<STUDENT, SCHOOL>((s1, s2) => s1.ID > 1) // where s1.id>1 .OrderBy<STUDENT, SCHOOL>((s1, s2) => s1.ID) //order by s1.id 多個order可以 .oderBy().orderby 疊加 .Skip(1) .Take(2) .Select<STUDENT, SCHOOL, classNew>((s1, s2) => new classNew() { NEWID = s1.ID, NEWNAME = s1.NAME, XX_NAME = s1.NAME })//select目前只支持這種寫法 .ToDynamic(); var jList3 = db.Queryable<STUDENT>() .JoinTable<STUDENT, SCHOOL>((s1, s2) => s1.SCH_ID == s2.ID) // left join School s2 on s1.id=s2.id .Where<STUDENT, SCHOOL>((s1, s2) => s1.ID > 1) // where s1.id>1 .OrderBy<STUDENT, SCHOOL>((s1, s2) => s1.ID) //order by s1.id 多個order可以 .oderBy().orderby 疊加 .Skip(1) .Take(2) .Select<STUDENT, classNew>(s1 => new classNew() { NEWID = s1.ID, NEWNAME = s1.NAME, XX_NAME = s1.NAME })//select目前只支持這種寫法 .ToDynamic(); }
添加/Add
//設置序列,程式起動時設置一次便可 OracleConfig.SequenceMapping = new List<PubModel.SequenceModel>() { new PubModel.SequenceModel(){ ColumnName="ID", TableName="STUDENT", Value="SEQ_D"} }; using (SqlSugarClient db = SugarDao.GetInstance())//開啟資料庫連接 { STUDENT s = new STUDENT() { NAME = "張" + new Random().Next(1, int.MaxValue) }; db.Insert(s); //插入一條記錄 (有主鍵也好,沒主鍵也好,有自增列也好都可以插進去) List<STUDENT> list = new List<STUDENT>() { new STUDENT() { NAME="張"+new Random().Next(1,int.MaxValue) }, new STUDENT() { NAME="張"+new Random().Next(1,int.MaxValue) } }; db.InsertRange(list); //批量插入 }
更新/Update
int id = 11; using (var db = SugarDao.GetInstance()) { //指定列更新 db.Update<SCHOOL>(new { name = "藍翔14" }, it => it.ID == 14); db.Update<SCHOOL, int>(new { name = "藍翔11 23 12", areaId=2 }, 11, 23, 12); db.Update<SCHOOL, string>(new { name = "藍翔2" }, new string[] { "11", "21" }); db.Update<SCHOOL>(new { name = "藍翔2" }, it => it.ID == id); //支持欄位指定列更新,適合動態許可權 var dic =new Dictionary<string, string>(); dic.Add("name","第十三條"); dic.Add("areaId", "1"); db.Update<SCHOOL,int>(dic, 13); db.Update<SCHOOL>(dic, it => it.ID == 13); //整個實體更新 db.Update(new SCHOOL { ID = 16, NAME = "藍翔16", AREAID=1 }); db.Update<SCHOOL>(new SCHOOL { ID = id, NAME = "藍翔18", AREAID=2 }, it => it.ID == 18); db.Update<SCHOOL>(new SCHOOL() { ID = 11, NAME = "xx" }); //設置不更新列 db.DisableUpdateColumns = new string[] { "CreateTime" };//設置CreateTime不更新 TESTUPDATECOLUMNS updObj = new TESTUPDATECOLUMNS() { VGUID = "542b5a27-6984-47c7-a8ee-359e483c8470", NAME = "xx", NAME2 = "xx2", IDENTITYFIELD = 1, CREATETIME = null }; //CreateTime將不會被更新 db.Update(updObj); //以前實現這種更新需要用指定列的方式實現,現在就簡單多了。 }