SysDbCore.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using SqlSugar;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using WMS.Util;
  7. namespace WMS.Core
  8. {
  9. public class SysDbCore
  10. {
  11. private static Util.ConnectionConfig SysDbSet = new Util.ConnectionConfig();
  12. private const string WMSBaseDB = "WMSBaseDB";
  13. public static SqlSugarClient GetDbCtx()
  14. {
  15. try
  16. {
  17. if (SysDbSet == null || string.IsNullOrWhiteSpace(SysDbSet.ConnectionString))
  18. {
  19. SysDbSet = ConfigHelper.GetConnectionConfigs().WMSConnectionConfig;
  20. if (SysDbSet == null || string.IsNullOrWhiteSpace(SysDbSet.ConnectionString))
  21. {
  22. throw SysExCore.ThrowDbSetError();
  23. }
  24. }
  25. return GetDbCtx(SysDbSet.ConnectionString, (DbType)SysDbSet.DataBaseType,(InitKeyType) SysDbSet.InitKey, SysDbSet.IsAutoCloseConn);
  26. //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);
  27. }
  28. catch (Exception ex)
  29. {
  30. throw SysExCore.ThrowDbConnError(ex);
  31. }
  32. }
  33. public static SqlSugarClient GetDbCtx(string connectionString,DbType dbType,InitKeyType initKey=InitKeyType.SystemTable,bool isAutoClose=true)
  34. {
  35. try
  36. {
  37. SqlSugarClient db = new SqlSugarClient(new SqlSugar.ConnectionConfig()
  38. {
  39. ConnectionString = connectionString,//必填, 数据库连接字符串
  40. DbType = dbType, //必填, 数据库类型
  41. IsAutoCloseConnection = isAutoClose, //默认false, 时候知道关闭数据库连接, 设置为true无需使用using或者Close操作
  42. InitKeyType = initKey //默认SystemTable, 字段信息读取, 如:该属性是不是主键,是不是标识列等等信息
  43. });
  44. if (SysSetCore.GetSysSet().IsLogSql)
  45. {
  46. db.Aop.OnLogExecuting = (sql, pars) =>
  47. {
  48. LogSql(sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
  49. };
  50. }
  51. return db;
  52. }
  53. catch (Exception ex)
  54. {
  55. throw SysExCore.ThrowDbConnError(ex);
  56. }
  57. }
  58. private static void LogSql(string Sql)
  59. {
  60. try
  61. {
  62. string Path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "LogSQL\\";
  63. if (!Directory.Exists(Path))
  64. {
  65. Directory.CreateDirectory(Path);
  66. }
  67. string filename = Path + "LogDB_" + DateTime.Now.ToString("yyyyMMdd");
  68. string LogText = DateTime.Now.ToString("yyyyMMddHHmmssffff") + " " + Sql;
  69. File.AppendAllLines(filename, new List<string>() { LogText });
  70. }
  71. catch
  72. {
  73. }
  74. }
  75. public static void DbConnExec(Action<SqlSugarClient> Execbody)
  76. {
  77. SqlSugarClient Ctx = null;
  78. try
  79. {
  80. Ctx = GetDbCtx();
  81. Execbody.Invoke(Ctx);
  82. }
  83. catch (Exception ex)
  84. {
  85. throw ex;
  86. }
  87. finally
  88. {
  89. Ctx?.Dispose();
  90. }
  91. }
  92. public static void DbTranExec(Action<SqlSugarClient> Execbody)
  93. {
  94. SqlSugarClient Ctx = null;
  95. try
  96. {
  97. Ctx = GetDbCtx();
  98. Ctx.Ado.BeginTran();
  99. Execbody.Invoke(Ctx);
  100. Ctx.Ado.CommitTran();
  101. }
  102. catch (Exception ex)
  103. {
  104. Ctx.Ado.RollbackTran();
  105. throw ex;
  106. }
  107. finally
  108. {
  109. Ctx?.Dispose();
  110. }
  111. }
  112. }
  113. }