DataProvider.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Data;
  7. using System.Data.SqlClient;
  8. using System.Reflection;
  9. using System.Web;
  10. namespace WindowsService
  11. {
  12. /// <summary>
  13. /// 数据访问基类
  14. /// </summary>
  15. public static class DataProvider
  16. {
  17. public static IDbConnection GetDbConnection()
  18. {
  19. string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Target"].ConnectionString;
  20. IDbConnection connection = new SqlConnection(connectionString);
  21. connection.Open();
  22. return connection;
  23. }
  24. /// <summary>
  25. /// 将 <see cref="DataTable"/> 的数据批量插入到数据库中。
  26. /// </summary>
  27. /// <param name="varlist">要批量插入的list。</param>
  28. /// <param name="batchSize">每批次写入的数据量。</param>
  29. public static void Insert<T>(string tableName, IEnumerable<T> varlist, int batchSize = 10000)
  30. {
  31. var dataTable = varlist.ToDataTable2<T>(null);
  32. if (dataTable.Rows.Count == 0)
  33. {
  34. return;
  35. }
  36. using (var connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Target"].ConnectionString))
  37. {
  38. try
  39. {
  40. connection.Open();
  41. //给表名加上前后导符
  42. using (var bulk = new SqlBulkCopy(connection, SqlBulkCopyOptions.KeepIdentity, null)
  43. {
  44. DestinationTableName = tableName,
  45. BatchSize = batchSize
  46. })
  47. {
  48. //循环所有列,为bulk添加映射
  49. foreach (DataColumn c in dataTable.Columns)
  50. {
  51. if (c.ColumnName == "id" || c.ColumnName == "ID" || c.ColumnName == "_id")
  52. { }
  53. else
  54. bulk.ColumnMappings.Add(c.ColumnName, c.ColumnName);
  55. }
  56. bulk.WriteToServer(dataTable);
  57. bulk.Close();
  58. }
  59. }
  60. catch (Exception exp)
  61. {
  62. throw exp;
  63. }
  64. finally
  65. {
  66. connection.Close();
  67. }
  68. }
  69. }
  70. public static void Clear()
  71. {
  72. using (var connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Target"].ConnectionString))
  73. {
  74. connection.Open();
  75. SqlCommand cmd = connection.CreateCommand();
  76. cmd.CommandText = "Delete From Users";
  77. cmd.ExecuteNonQuery();
  78. }
  79. }
  80. public static void E(Action<IDbCommand> act)
  81. {
  82. using (var connection = GetDbConnection())
  83. {
  84. act(connection.CreateCommand());
  85. }
  86. }
  87. /// <summary>
  88. /// 将 <see cref="DataTable"/> 的数据批量插入到数据库中。
  89. /// </summary>
  90. /// <param name="dataTable">要批量插入的 <see cref="DataTable"/>。</param>
  91. /// <param name="batchSize">每批次写入的数据量。</param>
  92. public static void Insert(string tableName, DataTable dataTable, int batchSize = 10000)
  93. {
  94. if (dataTable.Rows.Count == 0)
  95. {
  96. return;
  97. }
  98. using (var connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["target"].ConnectionString))
  99. {
  100. try
  101. {
  102. connection.Open();
  103. //给表名加上前后导符
  104. using (var bulk = new SqlBulkCopy(connection, SqlBulkCopyOptions.KeepIdentity, null)
  105. {
  106. DestinationTableName = tableName,
  107. BatchSize = batchSize
  108. })
  109. {
  110. //循环所有列,为bulk添加映射
  111. foreach (DataColumn c in dataTable.Columns)
  112. {
  113. bulk.ColumnMappings.Add(c.ColumnName, c.ColumnName.Trim());
  114. }
  115. bulk.WriteToServer(dataTable);
  116. bulk.Close();
  117. }
  118. }
  119. catch (Exception exp)
  120. {
  121. throw exp;
  122. }
  123. finally
  124. {
  125. connection.Close();
  126. }
  127. }
  128. }
  129. /// <summary>
  130. /// List<T> 转DataTable
  131. /// 使用方法: DataTable tb = infos.ToDataTable2<NationalInfo>(rec => new object[] { infos });
  132. /// </summary>
  133. /// <typeparam name="T"></typeparam>
  134. /// <param name="varlist"></param>
  135. /// <param name="fn"></param>
  136. /// <returns></returns>
  137. public static DataTable ToDataTable2<T>(this IEnumerable<T> varlist, CreateRowDelegate<T> fn)
  138. {
  139. DataTable dtReturn = new DataTable();
  140. PropertyInfo[] oProps = null;
  141. if (oProps == null)
  142. {
  143. oProps = (typeof(T)).GetProperties();
  144. foreach (PropertyInfo pi in oProps)
  145. {
  146. Type colType = pi.PropertyType;
  147. if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
  148. {
  149. colType = colType.GetGenericArguments()[0];
  150. }
  151. dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
  152. }
  153. }
  154. foreach (T rec in varlist)
  155. {
  156. DataRow dr = dtReturn.NewRow(); foreach (PropertyInfo pi in oProps)
  157. {
  158. dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue(rec, null);
  159. }
  160. dtReturn.Rows.Add(dr);
  161. }
  162. return (dtReturn);
  163. }
  164. public delegate object[] CreateRowDelegate<T>(T t);
  165. }
  166. }