123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- using SqlSugar;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using WMS.Util;
- namespace WMS.Core
- {
- public class SysDbCore
- {
- private static Util.ConnectionConfig SysDbSet = new Util.ConnectionConfig();
- private const string WMSBaseDB = "WMSBaseDB";
- public static SqlSugarClient GetDbCtx()
- {
-
- try
- {
- if (SysDbSet == null || string.IsNullOrWhiteSpace(SysDbSet.ConnectionString))
- {
- SysDbSet = ConfigHelper.GetConnectionConfigs().WMSConnectionConfig;
- 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<string>() { LogText });
- }
- catch
- {
- }
- }
- public static void DbConnExec(Action<SqlSugarClient> Execbody)
- {
- SqlSugarClient Ctx = null;
- try
- {
- Ctx = GetDbCtx();
- Execbody.Invoke(Ctx);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- Ctx?.Dispose();
- }
- }
- public static void DbTranExec(Action<SqlSugarClient> 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();
- }
- }
- }
- }
|