SysDbCoreold.cs 4.4 KB

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