GenericDataAccess.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using HNWD.Pregrant.Model;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Linq;
  7. using Dapper;
  8. namespace HNWD.Pregrant.DataAccess
  9. {
  10. public abstract class GenericDataAccess<Entity>
  11. where Entity : ModelBase, new()
  12. {
  13. public bool bSuccess = false;
  14. protected string Code = "";
  15. public Int32 Rows = 0;
  16. public string Message = "";
  17. /// <summary>
  18. /// 源列表
  19. /// </summary>
  20. public List<Entity> Entities = null;
  21. /// <summary>
  22. /// 查找源数据
  23. /// </summary>
  24. /// <param name="t">源实例</param>
  25. /// <param name="bAll"> 是否查询所有:true 为所有,false 为带条件 , 默认为:false </param>
  26. /// <returns>List<T>/></returns>
  27. protected List<Entity> Query(string strSql, Entity entity = null)
  28. {
  29. string fields = FieldNickGenerator<Entity>.GeneratFieldNick().ToString();
  30. strSql = string.Format(strSql, fields);
  31. ConnectorManager cm = new ConnectorManager();
  32. SqlConnection sql = cm.GetInstance();
  33. try
  34. {
  35. this.Entities = sql.Query<Entity>(strSql, entity).ToList();
  36. this.bSuccess = true;
  37. this.Rows = this.Entities.Count;
  38. this.Code = "OK!";
  39. this.Message = "成功获取请求数据!";
  40. }
  41. catch (Exception ex)
  42. {
  43. this.bSuccess = false;
  44. this.Code = "ERROR!";
  45. this.Rows = 0;
  46. this.Message = ex.Message;
  47. }
  48. finally
  49. {
  50. sql.Close();
  51. }
  52. return this.Entities;
  53. }
  54. /// <summary>
  55. /// 查找源数据
  56. /// </summary>
  57. /// <param name="t">源实例</param>
  58. /// <param name="bAll"> 是否查询所有:true 为所有,false 为带条件 , 默认为:false </param>
  59. /// <returns>List<T>/></returns>
  60. protected void Execute(string strSql, Entity entity)
  61. {
  62. ConnectorManager cm = new ConnectorManager();
  63. SqlConnection sql = cm.GetInstance();
  64. IDbTransaction transaction = sql.BeginTransaction();
  65. try
  66. {
  67. int i = sql.Execute(strSql, entity, transaction, null, null);
  68. this.bSuccess = true;
  69. this.Code = "OK!";
  70. this.Message = "命令执行成功!";
  71. this.Rows = 0;
  72. transaction.Commit();
  73. }
  74. catch (Exception ex)
  75. {
  76. this.bSuccess = false;
  77. this.Code = "ERROR!";
  78. this.Rows = 0;
  79. this.Message = ex.Message;
  80. transaction.Rollback();
  81. }
  82. finally
  83. {
  84. sql.Close();
  85. }
  86. transaction.Dispose();
  87. }
  88. /// <summary>
  89. /// 查找源数据
  90. /// </summary>
  91. /// <param name="t">源实例</param>
  92. /// <param name="bAll"> 是否查询所有:true 为所有,false 为带条件 , 默认为:false </param>
  93. /// <returns>List<T>/></returns>
  94. protected void Execute(string isql, string usql, string dsql)
  95. {
  96. ConnectorManager cm = new ConnectorManager();
  97. SqlConnection sql = cm.GetInstance();
  98. IDbTransaction transaction = sql.BeginTransaction();
  99. try
  100. {
  101. int i = 0, u = 0, d = 0;
  102. if (isql.Length > 0)
  103. {
  104. i = sql.Execute(isql, null, transaction, null, null);
  105. }
  106. if (usql.Length > 0)
  107. {
  108. u = sql.Execute(usql, null, transaction, null, null);
  109. }
  110. if (dsql.Length > 0)
  111. {
  112. d = sql.Execute(dsql, null, transaction, null, null);
  113. }
  114. this.bSuccess = true;
  115. this.Code = "OK!";
  116. this.Message = "命令执行成功!(i:" + i.ToString() + ",u:" + u.ToString() + ",d:" + d.ToString() + ")";
  117. this.Rows = i;
  118. transaction.Commit();
  119. }
  120. catch (Exception ex)
  121. {
  122. this.bSuccess = false;
  123. this.Code = "ERROR!";
  124. this.Rows = 0;
  125. this.Message = ex.Message;
  126. transaction.Rollback();
  127. }
  128. finally
  129. {
  130. sql.Close();
  131. }
  132. transaction.Dispose();
  133. }
  134. /// <summary>
  135. /// 查找源数据
  136. /// </summary>
  137. /// <param name="t">源实例</param>
  138. /// <param name="bAll"> 是否查询所有:true 为所有,false 为带条件 , 默认为:false </param>
  139. /// <returns>List<T>/></returns>
  140. protected void Execute(string isql, string usql, string dsql, string asql)
  141. {
  142. ConnectorManager cm = new ConnectorManager();
  143. SqlConnection sql = cm.GetInstance();
  144. IDbTransaction transaction = sql.BeginTransaction();
  145. try
  146. {
  147. int i = 0, u = 0, d = 0;
  148. if (isql.Length > 0)
  149. {
  150. i = sql.Execute(isql, null, transaction, null, null);
  151. }
  152. if (usql.Length > 0)
  153. {
  154. u = sql.Execute(usql, null, transaction, null, null);
  155. }
  156. if (dsql.Length > 0)
  157. {
  158. d = sql.Execute(dsql, null, transaction, null, null);
  159. }
  160. sql.Execute(asql, null, transaction, null, null);
  161. this.bSuccess = true;
  162. this.Code = "OK!";
  163. this.Message = "命令执行成功!(i:" + i.ToString() + ",u:" + u.ToString() + ",d:" + d.ToString() + ")";
  164. this.Rows = i;
  165. transaction.Commit();
  166. }
  167. catch (Exception ex)
  168. {
  169. this.bSuccess = false;
  170. this.Code = "ERROR!";
  171. this.Rows = 0;
  172. this.Message = ex.Message;
  173. transaction.Rollback();
  174. }
  175. finally
  176. {
  177. sql.Close();
  178. }
  179. transaction.Dispose();
  180. }
  181. }
  182. }