MsSqlContext.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using Ant.Core;
  2. using Ant.Core.Visitors;
  3. using Ant.Data;
  4. using Ant.DbExpressions;
  5. using Ant.Descriptors;
  6. using Ant.Entity;
  7. using Ant.Exceptions;
  8. using Ant.Infrastructure;
  9. using Ant.ORM;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Linq.Expressions;
  14. using System.Reflection;
  15. using System.Text;
  16. using System.Transactions;
  17. namespace Ant.SqlServer
  18. {
  19. /// <summary>
  20. ///
  21. /// </summary>
  22. public class MsSqlContext : DbContext
  23. {
  24. DbContextServiceProvider _dbContextServiceProvider;
  25. public MsSqlContext()
  26. {
  27. this.PagingMode = PagingMode.ROW_NUMBER;
  28. this._dbContextServiceProvider = new DbContextServiceProvider(PagingMode);
  29. }
  30. /// <summary>
  31. /// 数据库配置
  32. /// </summary>
  33. public string ConnectionString { get; set; }
  34. /// <summary>
  35. /// 数据库类型
  36. /// </summary>
  37. public override DatabaseType DatabaseType
  38. {
  39. get { return DatabaseType.MSSQLServer; }
  40. }
  41. /// <summary>
  42. /// 数据库配置
  43. /// </summary>
  44. public override DataAccess db
  45. {
  46. get { return new MSSqlDataAccess(ConnectionString); }
  47. }
  48. /// <summary>
  49. /// 开启事务
  50. /// </summary>
  51. public void BeginTransaction()
  52. {
  53. db.BeginTransaction();
  54. }
  55. /// <summary>
  56. /// 提交事务
  57. /// </summary>
  58. public void Commit()
  59. {
  60. db.Commit();
  61. }
  62. /// <summary>
  63. /// 回滚事务
  64. /// </summary>
  65. public void RollBack()
  66. {
  67. db.RollBack();
  68. }
  69. /// <summary>
  70. /// 调用说明
  71. /// public bool DataAcessPackageTrans(out string error)
  72. //{
  73. // var db = DBExtend;
  74. // //简化了事务写法,自动提交回滚
  75. // return DataAcessPackageTrans(db,(out string ex) =>
  76. // {
  77. // ex = "";
  78. // var product = new ProductData();
  79. // product.BarCode = "code" + DateTime.Now.Millisecond;
  80. // product.Number = 10;
  81. // db.InsertFromObj(product);
  82. // return false; //会回滚
  83. // }, out error);
  84. //}
  85. /// 使用DbTransaction封装事务(不推荐)
  86. /// </summary>
  87. /// <param name="db"></param>
  88. /// <param name="method"></param>
  89. /// <param name="error"></param>
  90. /// <returns></returns>
  91. public bool DataAcessPackageTrans(DataAccess sqldb, TransMethod method, out string error)
  92. {
  93. error = "";
  94. sqldb.BeginTransaction();
  95. try
  96. {
  97. var a = method(out error);
  98. if (!a)
  99. {
  100. sqldb.RollBack();
  101. return false;
  102. }
  103. sqldb.Commit();
  104. }
  105. catch (Exception ero)
  106. {
  107. error = "提交事务时发生错误:" + ero.Message;
  108. sqldb.RollBack();
  109. return false;
  110. }
  111. return true;
  112. }
  113. /// <summary>
  114. /// 调用说明
  115. /// public bool TransactionTest(out string error)
  116. //{
  117. // //简化了事务写法,自动提交回滚
  118. // return PackageTrans((out string ex) =>
  119. // {
  120. // ex = "";
  121. // var product = new ProductData();
  122. // product.BarCode = "code" + DateTime.Now.Millisecond;
  123. // product.Number = 10;
  124. // ProductDataManage.Instance.Add(product);
  125. // return false; //会回滚
  126. // }, out error);
  127. //}
  128. /// 使用TransactionScope封装事务[基本方法]分布式事务
  129. /// </summary>
  130. /// <param name="method"></param>
  131. /// <param name="error"></param>
  132. /// <returns></returns>
  133. public bool PackageTrans(TransMethod method, out string error)
  134. {
  135. error = "";
  136. using (var trans = new TransactionScope())
  137. {
  138. try
  139. {
  140. var a = method(out error);
  141. if (!a)
  142. {
  143. return false;
  144. }
  145. trans.Complete();
  146. }
  147. catch (Exception ero)
  148. {
  149. error = "提交事务时发生错误:" + ero.Message;
  150. return false;
  151. }
  152. }
  153. return true;
  154. }
  155. /// <summary>
  156. /// 数据库分页类型(SQL2005或者2008下,使用ROW_NUMBER()分页)(SQL2012下,使用OFFSET/FETCH NEXT分页)
  157. /// </summary>
  158. public PagingMode PagingMode { get; set; }
  159. /// <summary>
  160. /// 创建用哪种数据库解析SQL语句
  161. /// </summary>
  162. public override IDbContextServiceProvider DbContextServiceProvider
  163. {
  164. get { return this._dbContextServiceProvider; }
  165. }
  166. }
  167. }