Log : IDemos { public void Init() { Console.WriteLine(); using (var db = SugarDemoDao.GetInstance()) { var a1 = db.Queryable<Student>().Where(it => it.id == 1).ToList(); var a2 = db.Queryable<Student>().OrderBy(it => it.id).ToList(); } } public class SugarConfigs { public static Action<string, string> LogEventStarting = (sql, pars) => { Console.WriteLine(+ sql + " " + pars); using (var db = SugarDemoDao.GetInstance()) { //日志记录件事件里面用到数据库操作 IsEnableLogEvent一定要为false否则将引起死循环,并且要新开一个数据实例 像我这样写就没问题。 db.IsEnableLogEvent = false; db.ExecuteCommand(); } }; public static Action<string, string> LogEventCompleted = (sql, pars) => { Console.WriteLine(+ sql + " " + pars); }; } SqlSugar SugarDemoDao { public static SqlSugarClient GetInstance() { var db = new SqlSugarClient(SugarDao.ConnectionString); db.IsEnableLogEvent = true;//启用日志事件 db.LogEventStarting = SugarConfigs.LogEventStarting; db.LogEventCompleted = SugarConfigs.LogEventCompleted; return db; } } } }
View Code11、别名表功能
using System; using System.Collections.Generic; using System.Linq; using System.Text; using NewTest.Dao; using Models; using System.Data.SqlClient; using SqlSugar; namespace NewTest.Demos { MappingTable : IDemos { public void Init() { Console.WriteLine(); (var db = SugarDao.GetInstance()) { ).ToList();//查询的是 select * from student 而我的实体名称为V_Student } (var db = SugarFactory.GetInstance()) { var list = db.Queryable<V_Student>().ToList();//查询的是 select * from student 而我的实体名称为V_Student } } 全局配置类 SugarConfigs { List<KeyValue> MpList = new List<KeyValue>(){ , Value=}, , Value=}, , Value=} }; } SqlSugar实例工厂 SugarFactory { SugarFactory() { } public static SqlSugarClient GetInstance() { db = new SqlSugarClient(connection); db.SetMappingTables(SugarConfigs.MpList); db; } } } }
View Code12、行列过滤
using System; using System.Collections.Generic; using System.Linq; using System.Text; using NewTest.Dao; using Models; using System.Data.SqlClient; using SqlSugar; namespace NewTest.Demos { //行过滤加列过滤 Filter2 : IDemos { public void Init() { Console.WriteLine(); using (SqlSugarClient db = SugarDaoFilter.GetInstance())//开启数据库连接 { //设置走哪个过滤器 db.CurrentFilterKey = ; list = db.Queryable<Student>().ToJson(); //where id=1 , 可以查看id和name //设置走哪个过滤器 db.CurrentFilterKey = ; list2 = db.Queryable<Student>().ToJson(); //where id=2 , 可以查看name } } 扩展SqlSugarClient SugarDaoFilter { SugarDaoFilter() { } 页面所需要的过滤行 Dictionary<string, Func<KeyValueObj>> _filterParas = new Dictionary<string, Func<KeyValueObj>>() { { ,()=>{ , Value=new{ id=1}}; } }, { ,()=>{ , Value = new { id = 2 } }; } }, }; 页面所需要的过滤列 Dictionary<string, List<string>> _filterColumns = new Dictionary<string, List<string>>() { { ,,} }, { ,} }, }; public static SqlSugarClient GetInstance() { reval = new SqlSugarClient(connection); //支持sqlable和queryable reval.SetFilterFilterParas(_filterParas); //列过滤只支持queryable reval.SetFilterFilterParas(_filterColumns); return reval; } } } }
View Code13、自动排除非数据库列