DataProvider.cs 5.6 KB

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