using HH.WCS.Mobox3.AnGang; using Newtonsoft.Json; using SqlSugar; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Linq.Expressions; namespace HH.WCS.Mobox3.AnGang.util { //https://www.donet5.com/Home/Doc public class SqlHelper where T : class, new() { /// /// 如果用Oracle数据需要包Oracle.ManagedDataAccess/21.15.0,环境netframework 4.62,太新了4.8有的服务器安装不上去 /// /// /// public SqlSugarClient GetInstance(string url = "") { //创建数据库对象 SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = string.IsNullOrEmpty(url) ? Settings.SqlServer : url, //ConnectionString = @"Data Source=192.168.1.198\sql2008;Initial Catalog=OIMobox;User ID=sa;Password=sa@2015", DbType = DbType.SqlServer, //ConnectionString = @"Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=OIMobox)));User Id=system;Password=Am123123;", //DbType = DbType.Oracle, IsAutoCloseConnection = true, InitKeyType = InitKeyType.Attribute//从特性读取主键自增信息 }); //监控所有超过1秒的Sql db.Aop.OnLogExecuted = (sql, p) => { //执行时间超过1秒 if (db.Ado.SqlExecutionTime.TotalSeconds > 1) { Console.WriteLine(sql + "\r\n" + db.Utilities.SerializeObject(p.ToDictionary(it => it.ParameterName, it => it.Value))); //代码CS文件名 var fileName = db.Ado.SqlStackTrace.FirstFileName; //代码行数 var fileLine = db.Ado.SqlStackTrace.FirstLine; //方法名 var FirstMethodName = db.Ado.SqlStackTrace.FirstMethodName; } //相当于EF的 PrintToMiniProfiler }; //每次设置数值时都去除前导后导空格 db.Aop.DataExecuted = (value, entity) => { entity.EntityColumnInfos.ToList().ForEach(a => { var pvalue = entity.GetValue(a.PropertyName); if (pvalue != null && pvalue.GetType() == typeof(String)) { entity.SetValue(a.PropertyName, pvalue.ToString().Trim()); } }); }; //db.Aop.OnLogExecuting = (sql, pars) => { // Console.WriteLine(sql); //}; //据转换 (ExecuteCommand才会拦截,查询不行) //db.Aop.DataExecuting = (value, entity) => { // //var val=entity.EntityColumnInfo // Console.WriteLine(entity.EntityName); //}; return db; } } }