BulkCopy 位于 System.Data.SqlClient 命名空间, 允许你使用其他源的数据有效地批量加载 SQL Server 表.
属性:
BatchSize : 每个批处理中的行数. 在每个批处理结束时, 批处理中的行将发送到服务器.
BulkCopyTimeout: 超时之前可用于完成操作的秒数.
ColumnMappings: 返回 SqlBulkCopyColumnMapping 项的集合. 列映射定义数据源中的列和目标中的列之间的关系.
DestinationTableName: 服务器上的目标表的名称.
EnableStreaming: 启用或禁用 SqlBulkCopy 对象, 以流式传输 IDataReader 对象中的数据.
NotifyAfter: 定义生成通知事件之前要处理的行数.
今天测试了一下 BulkCopy 的速度, 100W 的数据量存储在 List<T > 中, 把 List 转化成 Datatable 后使用 BulkCopy 批量添加, 总平均时间仅为 7.5 秒. 以下是使用方式.
1. 首先是数据库操作方法
- /// <summary>
- /// 批量添加
- /// </summary>
- /// <param name="sqlBulkCopyColumnMappings"> 缓存数据表 DataTable 的列与 SQL Server 数据表列的映射关系 </param>
- /// <param name="dataTable"> 缓存数据表 DataTable</param>
- public static void SqlBulkCopy(List<SqlBulkCopyColumnMapping> sqlBulkCopyColumnMappings, DataTable dataTable)
- {
- using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(datalink.ConnectionString))
- {
- sqlBulkCopy.DestinationTableName = "Student";
- sqlBulkCopy.BatchSize = 10;
- foreach (var item in sqlBulkCopyColumnMappings)
- {
- sqlBulkCopy.ColumnMappings.Add(item.SourceColumn, item.DestinationColumn);
- }
- sqlBulkCopy.WriteToServer(dataTable);
- }
- }
说明: 如果模型类 Model 与数据表字段一一对应, 第一个参数 sqlBulkCopyColumnMappings 使用空构造方法创建一个即可. 由于这是 Student 表的数据库操作类, 所以 DestinationTableName 直接赋值 Student.
2. 可能需要以下辅助类, 把 List 数据转化成 DataTable
- /// <summary>
- /// List 集合转换成 DataTable
- /// </summary>
- /// <param name="list"></param>
- /// <returns></returns>
- public static DataTable ListToDataTable(IList list)
- {
- DataTable result = new DataTable();
- if (list.Count> 0)
- {
- PropertyInfo[] propertys = list[0].GetType().GetProperties();
- foreach (PropertyInfo pi in propertys)
- {
- // 获取类型
- Type colType = pi.PropertyType;
- // 当类型为 Nullable<> 时
- if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
- {
- colType = colType.GetGenericArguments()[0];
- }
- result.Columns.Add(pi.Name, colType);
- }
- for (int i = 0; i < list.Count; i++)
- {
- ArrayList tempList = new ArrayList();
- foreach (PropertyInfo pi in propertys)
- {
- object obj = pi.GetValue(list[i], null);
- tempList.Add(obj);
- }
- object[] array = tempList.ToArray();
- result.LoadDataRow(array, true);
- }
- }
- return result;
- }
- View Code
来源: http://www.bubuko.com/infodetail-2781399.html