123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- using SqlSugar;
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.IO;
- using System.Linq;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Text;
- using System.Threading.Tasks;
- namespace WMS.Core
- {
- internal class SysDbCoreold
- {
- private static SysDbInfo SysDbSet=new SysDbInfo();
- private const string WMSBaseDB = "WMSBaseDB";
- public static SqlSugarClient GetDbCtx()
- {
- try
- {
- if (SysDbSet == null || string.IsNullOrWhiteSpace(SysDbSet.ConnectionString))
- {
- SysDbSet = SysDbInfo.GetConfig();
- if (SysDbSet == null || string.IsNullOrWhiteSpace(SysDbSet.ConnectionString))
- {
- throw SysExCore.ThrowDbSetError();
- }
- }
- SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
- {
- ConnectionString = SysDbSet.ConnectionString,//必填, 数据库连接字符串
- DbType = SysDbSet.DataBaseType, //必填, 数据库类型
- IsAutoCloseConnection = SysDbSet.IsAutoCloseConn, //默认false, 时候知道关闭数据库连接, 设置为true无需使用using或者Close操作
- InitKeyType = SysDbSet.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);
- }
- }
- public static void SaveDbSet(SysDbInfo DbSetData, string PathFileName)
- {
- try
- {
- if (DbSetData == null)
- {
- throw new Exception("数据库连接设定为空。");
- }
- if (string.IsNullOrWhiteSpace(PathFileName))
- {
- throw new Exception("保存路径为空。");
- }
- DbSetData.DbSetNo = WMSBaseDB;
- BinaryFormatter serializer = new BinaryFormatter();
- using (MemoryStream memStream = new MemoryStream())
- {
- serializer.Serialize(memStream, DbSetData);
- File.WriteAllText(PathFileName, Convert.ToBase64String(memStream.ToArray()));
- }
- }
- catch (Exception ex)
- {
- throw 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();
- }
- }
- }
- }
|