SqlConnectionExtension.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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.Text;
  8. using System.Threading.Tasks;
  9. namespace Ant.Service.Utility
  10. {
  11. public enum Gender
  12. {
  13. Man = 1,
  14. Woman
  15. }
  16. public class User
  17. {
  18. public Guid Id { get; set; }
  19. public string Name { get; set; }
  20. public Gender? Gender { get; set; }
  21. public int? Age { get; set; }
  22. public int? CityId { get; set; }
  23. public DateTime? OpTime { get; set; }
  24. }
  25. public static class SqlConnectionExtension
  26. {
  27. /// <summary>
  28. /// 测试代码
  29. /// </summary>
  30. public static void test()
  31. {
  32. List<User> usersToInsert = new List<User>();
  33. usersToInsert.Add(new User() { Id = Guid.NewGuid(), Name = "so1", Gender = Gender.Man, Age = 18, CityId = 1, OpTime = DateTime.Now });
  34. usersToInsert.Add(new User() { Id = Guid.NewGuid(), Name = "so2", Gender = Gender.Man, Age = 19, CityId = 2, OpTime = DateTime.Now });
  35. usersToInsert.Add(new User() { Id = Guid.NewGuid(), Name = "so3", Gender = Gender.Man, Age = 20, CityId = 3, OpTime = DateTime.Now });
  36. usersToInsert.Add(new User() { Id = Guid.NewGuid(), Name = "so4", Gender = Gender.Man, Age = 21, CityId = 4, OpTime = DateTime.Now });
  37. using (SqlConnection conn = new SqlConnection("Data Source = .;Initial Catalog = Chloe;Integrated Security = SSPI;"))
  38. {
  39. conn.BulkCopy(usersToInsert, 20000, "Users");
  40. }
  41. }
  42. /// <summary>
  43. /// 使用 SqlBulkCopy 向 destinationTableName 表插入数据
  44. /// </summary>
  45. /// <typeparam name="TModel">必须拥有与目标表所有字段对应属性</typeparam>
  46. /// <param name="conn"></param>
  47. /// <param name="modelList">要插入的数据</param>
  48. /// <param name="batchSize">SqlBulkCopy.BatchSize每次传输的行数</param>
  49. /// <param name="destinationTableName">如果为 null,则使用 TModel 名称作为 destinationTableName</param>
  50. /// <param name="bulkCopyTimeout">SqlBulkCopy.BulkCopyTimeout</param>
  51. /// <param name="externalTransaction">要使用的事务</param>
  52. public static void BulkCopy<TModel>(this SqlConnection conn, List<TModel> modelList, int batchSize, string destinationTableName = null, int? bulkCopyTimeout = null, SqlTransaction externalTransaction = null)
  53. {
  54. bool shouldCloseConnection = false;
  55. if (string.IsNullOrEmpty(destinationTableName))
  56. destinationTableName = typeof(TModel).Name;
  57. DataTable dtToWrite = ToSqlBulkCopyDataTable(modelList, conn, destinationTableName);
  58. SqlBulkCopy sbc = null;
  59. try
  60. {
  61. if (externalTransaction != null)
  62. sbc = new SqlBulkCopy(conn, SqlBulkCopyOptions.Default, externalTransaction);
  63. else
  64. sbc = new SqlBulkCopy(conn);
  65. using (sbc)
  66. {
  67. sbc.BatchSize = batchSize;//每次传输的行数
  68. sbc.DestinationTableName = destinationTableName;
  69. if (bulkCopyTimeout != null)
  70. sbc.BulkCopyTimeout = bulkCopyTimeout.Value;
  71. if (conn.State != ConnectionState.Open)
  72. {
  73. shouldCloseConnection = true;
  74. conn.Open();
  75. }
  76. sbc.WriteToServer(dtToWrite);
  77. }
  78. }
  79. finally
  80. {
  81. if (shouldCloseConnection && conn.State == ConnectionState.Open)
  82. conn.Close();
  83. }
  84. }
  85. /// <summary>
  86. ///
  87. /// </summary>
  88. /// <typeparam name="TModel"></typeparam>
  89. /// <param name="modelList"></param>
  90. /// <param name="conn"></param>
  91. /// <param name="tableName"></param>
  92. /// <returns></returns>
  93. public static DataTable ToSqlBulkCopyDataTable<TModel>(List<TModel> modelList, SqlConnection conn, string tableName)
  94. {
  95. DataTable dt = new DataTable();
  96. Type modelType = typeof(TModel);
  97. List<SysColumn> columns = GetTableColumns(conn, tableName);
  98. List<PropertyInfo> mappingProps = new List<PropertyInfo>();
  99. var props = modelType.GetProperties();
  100. for (int i = 0; i < columns.Count; i++)
  101. {
  102. var column = columns[i];
  103. PropertyInfo mappingProp = props.Where(a => a.Name == column.Name).FirstOrDefault();
  104. if (mappingProp == null)
  105. throw new Exception(string.Format("model 类型 '{0}'未定义与表 '{1}' 列名为 '{2}' 映射的属性", modelType.FullName, tableName, column.Name));
  106. mappingProps.Add(mappingProp);
  107. Type dataType = GetUnderlyingType(mappingProp.PropertyType);
  108. if (dataType.IsEnum)
  109. dataType = typeof(int);
  110. dt.Columns.Add(new DataColumn(column.Name, dataType));
  111. }
  112. foreach (var model in modelList)
  113. {
  114. DataRow dr = dt.NewRow();
  115. for (int i = 0; i < mappingProps.Count; i++)
  116. {
  117. PropertyInfo prop = mappingProps[i];
  118. object value = prop.GetValue(model);
  119. if (GetUnderlyingType(prop.PropertyType).IsEnum)
  120. {
  121. if (value != null)
  122. value = (int)value;
  123. }
  124. dr[i] = value ?? DBNull.Value;
  125. }
  126. dt.Rows.Add(dr);
  127. }
  128. return dt;
  129. }
  130. /// <summary>
  131. /// 获取表中所有列
  132. /// </summary>
  133. /// <param name="sourceConn"></param>
  134. /// <param name="tableName"></param>
  135. /// <returns></returns>
  136. static List<SysColumn> GetTableColumns(SqlConnection sourceConn, string tableName)
  137. {
  138. string sql = string.Format("select * from syscolumns inner join sysobjects on syscolumns.id=sysobjects.id where sysobjects.xtype='U' and sysobjects.name='{0}' order by syscolumns.colid asc", tableName);
  139. List<SysColumn> columns = new List<SysColumn>();
  140. using (SqlConnection conn = (SqlConnection)((ICloneable)sourceConn).Clone())
  141. {
  142. conn.Open();
  143. SqlCommand command = new SqlCommand(sql, conn);
  144. using (var reader = command.ExecuteReader())
  145. {
  146. while (reader.Read())
  147. {
  148. SysColumn column = new SysColumn();
  149. column.Name = reader["name"].ToString();
  150. column.ColOrder = reader["colorder"].ToInt32();
  151. columns.Add(column);
  152. }
  153. }
  154. conn.Close();
  155. }
  156. return columns;
  157. }
  158. static Type GetUnderlyingType(Type type)
  159. {
  160. Type unType = Nullable.GetUnderlyingType(type); ;
  161. if (unType == null)
  162. unType = type;
  163. return unType;
  164. }
  165. class SysColumn
  166. {
  167. public string Name { get; set; }
  168. public int ColOrder { get; set; }
  169. }
  170. }
  171. }