DbcontextExt.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Transactions;
  6. using System.Data.Linq;
  7. using System.Data.SqlClient;
  8. using System.Data;
  9. using System.ComponentModel;
  10. using System.Linq.Expressions;
  11. namespace Bowin.Common.Linq.Sql
  12. {
  13. /// <summary>
  14. /// 扩展linqtoSql对表操作以及事务的简化方法
  15. /// creator:wufs
  16. /// </summary>
  17. public static class DbcontextExt
  18. {
  19. /// <summary>
  20. /// 新增数据
  21. /// </summary>
  22. /// <typeparam name="T">表实体</typeparam>
  23. /// <param name="databasecontext"></param>
  24. /// <param name="Entity">表实体对象</param>
  25. public static void Insert<T>(this DataContext databasecontext, T Entity) where T : class
  26. {
  27. databasecontext.GetTable<T>().InsertOnSubmit(Entity);
  28. databasecontext.SubmitChanges();
  29. }
  30. /// <summary>
  31. /// 更新数据
  32. /// </summary>
  33. /// <typeparam name="T">表实体</typeparam>
  34. /// <param name="databasecontext"></param>
  35. /// <param name="aWhere">条件</param>
  36. /// <param name="aEntity">更新字段描述</param>
  37. public static void Update<T>(this DataContext databasecontext, Func<T, bool> aWhere, Action<T> aEntity) where T : class
  38. {
  39. var sqlrows = databasecontext.GetTable<T>().AsQueryable().Where(aWhere);
  40. foreach (var row in sqlrows)
  41. {
  42. aEntity(row);
  43. }
  44. databasecontext.SubmitChanges();
  45. }
  46. public static void Update<T>(this DataContext databasecontext, Func<T, bool> aWhere) where T : class
  47. {
  48. }
  49. /// <summary>
  50. /// 删除数据
  51. /// </summary>
  52. /// <typeparam name="T">表实体</typeparam>
  53. /// <param name="databasecontext"></param>
  54. /// <param name="aWhere">条件</param>
  55. public static void Delete<T>(this DataContext databasecontext, Func<T, bool> aWhere) where T : class
  56. {
  57. var sqlrows = databasecontext.GetTable<T>().AsQueryable().Where(aWhere);
  58. databasecontext.GetTable<T>().DeleteAllOnSubmit<T>(sqlrows);
  59. databasecontext.SubmitChanges();
  60. }
  61. /// <summary>
  62. /// 获得表数据
  63. /// </summary>
  64. /// <typeparam name="T">表实体</typeparam>
  65. /// <param name="databasecontext"></param>
  66. /// <param name="aWhere">条件</param>
  67. /// <returns>表的结果数据列</returns>
  68. public static List<T> Get<T>(this DataContext databasecontext, Func<T, bool> aWhere) where T : class
  69. {
  70. return databasecontext.GetTable<T>().AsQueryable().Where(aWhere).ToList();
  71. }
  72. /// <summary>
  73. /// 带事务新增数据
  74. /// 必需调用_CommitTran()提交方法才生效
  75. /// </summary>
  76. /// <typeparam name="T">表实体</typeparam>
  77. /// <param name="databasecontext"></param>
  78. /// <param name="Entity">表实体对象</param>
  79. public static void _Insert<T>(this DataContext databasecontext, T Entity) where T : class
  80. {
  81. databasecontext.GetTable<T>().InsertOnSubmit(Entity);
  82. }
  83. /// <summary>
  84. /// 带事务更新数据
  85. /// 必需调用_CommitTran()提交方法才生效
  86. /// </summary>
  87. /// <typeparam name="T">表实体</typeparam>
  88. /// <param name="databasecontext"></param>
  89. /// <param name="aWhere">条件</param>
  90. /// <param name="aEntity">更新字段描述</param>
  91. public static void _Update<T>(this DataContext databasecontext,Func<T, bool> aWhere, Action<T> aEntity) where T : class
  92. {
  93. var sqlrows = databasecontext.GetTable<T>().AsQueryable().Where(aWhere);
  94. foreach (var row in sqlrows)
  95. {
  96. aEntity(row);
  97. }
  98. }
  99. public static void _Update<T>(this DataContext databasecontext, Func<T, bool> aWhere) where T : class
  100. {
  101. }
  102. /// <summary>
  103. /// 带事务删除数据
  104. /// 必需调用_CommitTran()提交方法才生效
  105. /// </summary>
  106. /// <typeparam name="T">表实体</typeparam>
  107. /// <param name="databasecontext"></param>
  108. /// <param name="aWhere">条件</param>
  109. public static void _Delete<T>(this DataContext databasecontext, Func<T, bool> aWhere) where T : class
  110. {
  111. var sqlrows = databasecontext.GetTable<T>().AsQueryable().Where(aWhere);
  112. databasecontext.GetTable<T>().DeleteAllOnSubmit<T>(sqlrows);
  113. }
  114. /// <summary>
  115. /// 带事务操作提交方法
  116. /// </summary>
  117. /// <param name="databasecontext"></param>
  118. public static void _CommitTran(this DataContext databasecontext)
  119. {
  120. databasecontext.SubmitChanges();
  121. }
  122. /// <summary>
  123. /// 分布式事务应用方法
  124. /// </summary>
  125. /// <param name="actions"></param>
  126. public static void _TranExecute(params Action[] actions)
  127. {
  128. _TranExecute(System.Transactions.IsolationLevel.ReadCommitted, 60, actions);
  129. }
  130. /// <summary>
  131. /// 分布式事务应用方法
  132. /// </summary>
  133. public static void _TranExecute(int timeOut, params Action[] actions)
  134. {
  135. _TranExecute(System.Transactions.IsolationLevel.ReadCommitted, timeOut, actions);
  136. }
  137. /// <summary>
  138. /// 分布式事务应用方法
  139. /// </summary>
  140. public static void _TranExecute(System.Transactions.IsolationLevel level, params Action[] actions)
  141. {
  142. _TranExecute(level, 60, actions);
  143. }
  144. /// <summary>
  145. /// 分布式事务应用方法
  146. /// </summary>
  147. public static void _TranExecute(System.Transactions.IsolationLevel level, int timeOut, params Action[] actions)
  148. {
  149. if (actions == null || actions.Length == 0)
  150. {
  151. return;
  152. }
  153. TransactionOptions options = new TransactionOptions();
  154. options.IsolationLevel = level;
  155. options.Timeout = new TimeSpan(0, 0, timeOut);
  156. using (TransactionScope tran = new TransactionScope(TransactionScopeOption.Required, options))
  157. {
  158. Array.ForEach<Action>(actions, a => a());
  159. tran.Complete();
  160. }
  161. }
  162. public static DataSet ExecuteQuerySqlDS(this DataContext databasecontext,CommandType comType, string sql,Dictionary<string,object> dicobjs)
  163. {
  164. SqlCommand com = new SqlCommand();
  165. com.CommandText = sql;
  166. com.Connection = databasecontext.Connection as SqlConnection;
  167. com.CommandType = comType;
  168. com.CommandTimeout = databasecontext.CommandTimeout;
  169. foreach (var obj in dicobjs)
  170. {
  171. SqlParameter sqlParam = new SqlParameter();
  172. sqlParam.ParameterName = obj.Key;
  173. sqlParam.Value = obj.Value;
  174. com.Parameters.Add(sqlParam);
  175. }
  176. SqlDataAdapter sqlad = new SqlDataAdapter(com);
  177. DataSet ds = new DataSet("root");
  178. sqlad.Fill(ds);
  179. return ds;
  180. }
  181. public static DataSet ExecuteQuerySqlDS(this DataContext databasecontext,CommandType comType, string sql)
  182. {
  183. var dics = new Dictionary<string, object>();
  184. return databasecontext.ExecuteQuerySqlDS(comType, sql, dics);
  185. }
  186. public static DataSet ExecuteQuerySqlDS(this DataContext databasecontext, string sql, Dictionary<string, object> dics)
  187. {
  188. return databasecontext.ExecuteQuerySqlDS(CommandType.Text, sql, dics);
  189. }
  190. public static DataSet ExecuteQuerySqlDS(this DataContext databasecontext, string sql)
  191. {
  192. var dics=new Dictionary<string,object>();
  193. return databasecontext.ExecuteQuerySqlDS(CommandType.Text,sql, dics);
  194. }
  195. public static DataTable GetBulkCopyDataTable(this DataContext databasecontext, string TableName)
  196. {
  197. DataTable resultdt = new DataTable(TableName.Replace("[","").Replace("]",""));
  198. string sql = string.Format("select * from {0} where 1=2",TableName);
  199. SqlCommand com = new SqlCommand();
  200. com.CommandText = sql;
  201. com.Connection = databasecontext.Connection as SqlConnection;
  202. com.CommandType = CommandType.Text;
  203. com.CommandTimeout = databasecontext.CommandTimeout;
  204. SqlDataAdapter sqlad = new SqlDataAdapter(com);
  205. sqlad.Fill(resultdt);
  206. return resultdt;
  207. }
  208. public static bool ExecuteBulkCopy(this DataContext databasecontext, DataTable dt)
  209. {
  210. bool result = true;
  211. var sconn = databasecontext.Connection as SqlConnection;
  212. if (sconn.State == ConnectionState.Closed)
  213. {
  214. sconn.Open();
  215. }
  216. var stran = sconn.BeginTransaction();
  217. try
  218. {
  219. using (SqlBulkCopy bcp = new SqlBulkCopy(sconn, SqlBulkCopyOptions.Default, stran))
  220. {
  221. bcp.DestinationTableName = dt.TableName;
  222. bcp.WriteToServer(dt);
  223. stran.Commit();
  224. if (sconn.State == ConnectionState.Open)
  225. sconn.Close();
  226. }
  227. }
  228. catch (Exception ex)
  229. {
  230. result = false;
  231. stran.Rollback();
  232. if (sconn.State == ConnectionState.Open)
  233. sconn.Close();
  234. }
  235. return result;
  236. }
  237. }
  238. }