SqlSugarHelper.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using SqlSugar;
  2. namespace ServiceCenter.SqlSugars
  3. {
  4. /// <summary>
  5. /// ORM
  6. /// </summary>
  7. public class SqlSugarHelper
  8. {
  9. /// <summary>
  10. /// 数据库连接
  11. /// </summary>
  12. private static SqlSugarScope? _Db { get; set; } = null;
  13. /// <summary>
  14. /// 默认数据库连接Key
  15. /// </summary>
  16. private static string _Default { get; set; } = "";
  17. /// <summary>
  18. /// PLC据库连接Key
  19. /// </summary>
  20. private static string _PLC { get; set; } = "";
  21. /// <summary>
  22. /// Dlog数据库连接Key
  23. /// </summary>
  24. private static string _Dlog { get; set; } = "";
  25. /// <summary>
  26. /// 设置数据库连接Key
  27. /// </summary>
  28. /// <param name="configId">默认多租户ID</param>
  29. public static void SetDefault(string configId)
  30. {
  31. _Default = configId;
  32. }
  33. /// <summary>
  34. /// 设置 Dlog数据库连接Key
  35. /// </summary>
  36. /// <param name="configId">多租户Dlog ID</param>
  37. public static void SetDlog(string configId)
  38. {
  39. _Dlog = configId;
  40. }
  41. /// <summary>
  42. /// 设置 plc数据库连接Key
  43. /// </summary>
  44. /// <param name="configId">多租户Dlog ID</param>
  45. public static void SetPLC(string configId)
  46. {
  47. _PLC = configId;
  48. }
  49. /// <summary>
  50. /// 默认数据库连接Key
  51. /// </summary>
  52. public SqlSugarScopeProvider Default
  53. {
  54. get
  55. {
  56. if (_Default == "") throw new Exception("请调用[SqlSugarHelper.SetDefault]方法设置默认数据库连接");
  57. if (_Db == null) throw new Exception("请调用[SqlSugarHelper.SetDb]方法设置设置数据库连接");
  58. return _Db.GetConnectionScope(_Default);
  59. }
  60. }
  61. /// <summary>
  62. /// Dlog数据库连接Key
  63. /// </summary>
  64. public SqlSugarScopeProvider Dlog
  65. {
  66. get
  67. {
  68. if (_Dlog == "") throw new Exception("请调用[SqlSugarHelper.SetDlog]方法设置默认数据库连接");
  69. if (_Db == null) throw new Exception("请调用[SqlSugarHelper.SetDb]方法设置设置数据库连接");
  70. return _Db.GetConnectionScope(_Dlog);
  71. }
  72. }
  73. /// <summary>
  74. /// plc数据库连接Key
  75. /// </summary>
  76. public SqlSugarScopeProvider PLC
  77. {
  78. get
  79. {
  80. if (_PLC == "") throw new Exception("请调用[SqlSugarHelper.SetPLC]方法设置默认数据库连接");
  81. if (_Db == null) throw new Exception("请调用[SqlSugarHelper.SetDb]方法设置设置数据库连接");
  82. return _Db.GetConnectionScope(_PLC);
  83. }
  84. }
  85. /// <summary>
  86. /// 设置数据库连接
  87. /// </summary>
  88. /// <param name="sqlSugarScope"></param>
  89. public static void SetDb(SqlSugarScope sqlSugarScope)
  90. {
  91. _Db = sqlSugarScope;
  92. }
  93. /// <summary>
  94. /// 数据库连接
  95. /// 注意需要
  96. /// </summary>
  97. public SqlSugarScope Connect
  98. {
  99. get
  100. {
  101. return _Db ?? throw new Exception("请调用[SqlSugarHelper.SetDb]方法设置设置数据库连接");
  102. }
  103. }
  104. /// <summary>
  105. /// 执行事务
  106. /// </summary>
  107. /// <param name="act"></param>
  108. /// <exception cref="Exception"></exception>
  109. public static void Do(Action<SqlSugarHelper> act)
  110. {
  111. if (_Db == null) throw new Exception("请调用[SqlSugarHelper.SetDb]方法设置设置数据库连接");
  112. var db = new SqlSugarHelper();
  113. try
  114. {
  115. db.Connect.BeginTran();//开始事务
  116. act(db);//执行委托
  117. db.Connect.CommitTran();//提交事务
  118. }
  119. catch (Exception ex)
  120. {
  121. db.Connect.RollbackTran();//回滚事务
  122. //Console.WriteLine($"事务回滚记录:{ex.Message}:{ex.StackTrace}");
  123. throw new Exception(ex.Message);
  124. }
  125. }
  126. }
  127. }