using SqlSugar; using System; using System.Collections.Generic; using System.IO; using System.Linq; using WMS.Util; namespace WMS.Core { public class WCSDbCore { private static Util.ConnectionConfig SysDbSet = new Util.ConnectionConfig(); public static List ConnectionConfigs = new List(); static WCSDbCore() { var wcsconfig = ConfigHelper.GetConnectionConfigs().WCSConnectionConfig; ConnectionConfigs.Add(new SqlSugar.ConnectionConfig() { ConnectionString = wcsconfig.ConnectionString,//连接符字串 DbType = (DbType)wcsconfig.DataBaseType,//数据库类型 IsAutoCloseConnection = wcsconfig.IsAutoCloseConn //不设成true要手动close); }); //ConnectionConfigs.Add(new ConnectionConfig() //{ // ConnectionString = SysDbInfo.GetConfig("wcsdatabaseconfig").ConnectionString,//连接符字串 // DbType = SysDbInfo.GetConfig("wcsdatabaseconfig").DataBaseType,//数据库类型 // IsAutoCloseConnection = SysDbInfo.GetConfig("wcsdatabaseconfig").IsAutoCloseConn //不设成true要手动close); //}); } public static SqlSugarScope Db = new SqlSugarScope(new SqlSugar.ConnectionConfig() { ConnectionString = ConfigHelper.GetConnectionConfigs().WCSConnectionConfig.ConnectionString,//连接符字串 DbType = (DbType)ConfigHelper.GetConnectionConfigs().WCSConnectionConfig.DataBaseType,//数据库类型 IsAutoCloseConnection = ConfigHelper.GetConnectionConfigs().WCSConnectionConfig.IsAutoCloseConn //不设成true要手动close }, db => { //(A)全局生效配置点,一般AOP和程序启动的配置扔这里面 ,所有上下文生效 //调试SQL事件,可以删掉 db.Aop.OnLogExecuting = (sql, pars) => { Console.WriteLine(sql);//输出sql,查看执行sql 性能无影响 //获取原生SQL推荐 5.1.4.63 性能OK //UtilMethods.GetNativeSql(sql,pars) //获取无参数化SQL 对性能有影响,特别大的SQL参数多的,调试使用 //UtilMethods.GetSqlString(DbType.SqlServer,sql,pars) }; //多个配置就写下面 //db.Ado.IsDisableMasterSlaveSeparation=true; //注意多租户 有几个设置几个 //db.GetConnection(i).Aop }); public static SqlSugarClient GetDbCtx() { try { if (SysDbSet == null || string.IsNullOrWhiteSpace(SysDbSet.ConnectionString)) { SysDbSet = ConfigHelper.GetConnectionConfigs().WCSConnectionConfig; if (SysDbSet == null || string.IsNullOrWhiteSpace(SysDbSet.ConnectionString)) { throw SysExCore.ThrowDbSetError(); } } return GetDbCtx(SysDbSet.ConnectionString, (DbType)SysDbSet.DataBaseType, (InitKeyType)SysDbSet.InitKey, SysDbSet.IsAutoCloseConn); //return GetDbCtx("User ID=C##KWMS;Password=123456;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.0.200)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SID=ORCL)))", SysDbSet.DataBaseType, SysDbSet.InitKey, SysDbSet.IsAutoCloseConn); } catch (Exception ex) { throw SysExCore.ThrowDbConnError(ex); } } public static SqlSugarClient GetDbCtx(string connectionString, DbType dbType, InitKeyType initKey = InitKeyType.SystemTable, bool isAutoClose = true) { try { SqlSugarClient db = new SqlSugarClient(new SqlSugar. ConnectionConfig() { ConnectionString = connectionString,//必填, 数据库连接字符串 DbType = dbType, //必填, 数据库类型 IsAutoCloseConnection = isAutoClose, //默认false, 时候知道关闭数据库连接, 设置为true无需使用using或者Close操作 InitKeyType = initKey //默认SystemTable, 字段信息读取, 如:该属性是不是主键,是不是标识列等等信息 }); if (SysSetCore.GetSysSet().IsLogSql) { db.Aop.OnLogExecuting = (sql, pars) => { LogSql(sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value))); }; } return db; } catch (Exception ex) { throw SysExCore.ThrowDbConnError(ex); } } private static void LogSql(string Sql) { try { string Path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "LogSQL\\"; if (!Directory.Exists(Path)) { Directory.CreateDirectory(Path); } string filename = Path + "LogDB_" + DateTime.Now.ToString("yyyyMMdd"); string LogText = DateTime.Now.ToString("yyyyMMddHHmmssffff") + " " + Sql; File.AppendAllLines(filename, new List() { LogText }); } catch { } } public static void DbConnExec(Action Execbody) { SqlSugarClient Ctx = null; try { Ctx = GetDbCtx(); Execbody.Invoke(Ctx); } catch (Exception ex) { throw ex; } finally { Ctx?.Dispose(); } } public static void DbTranExec(Action Execbody) { SqlSugarClient Ctx = null; try { Ctx = GetDbCtx(); Ctx.Ado.BeginTran(); Execbody.Invoke(Ctx); Ctx.Ado.CommitTran(); } catch (Exception ex) { Ctx.Ado.RollbackTran(); throw ex; } finally { Ctx?.Dispose(); } } } }