当前位置:   article > 正文

C# 使用DapperExtensions实现大批数据插入_dapper批量插入数据库

dapper批量插入数据库

要插入5万多条数据

第一、使用Dapper,遥遥无期

  1. public static int InsertByTran(List<Model.LidarPointDeformation> list, string lidarcode)
  2. {
  3. string connectionString = DataBaseApp.GetBusinessDbStringByLidarcode(lidarcode);
  4. using (SqlConnection connection = new SqlConnection(connectionString))
  5. {
  6. connection.Open();
  7. int record = 0;
  8. using (var trans = connection.BeginTransaction())
  9. {
  10. try
  11. {
  12. var sql = "INSERT INTO LidarPointDeformation(id, pointId, row, col, reffile, deformation, lidarcode, monitoringtime) VALUES (@id,@pointId,@row,@col,@reffile,@deformation,@lidarcode,@monitoringtime)";
  13. record = connection.Execute(sql, list, trans, 60, CommandType.Text);
  14. }
  15. catch (DataException e)
  16. {
  17. trans.Rollback();
  18. throw e;
  19. }
  20. trans.Commit();
  21. }
  22. return record;
  23. }
  24. }

第二、使用sql语句拼接,但是最多只支持1000条,差强人意

  1. public static void InsertBySQL(List<Model.LidarPointDeformation> list, string lidarcode)
  2. {
  3. try
  4. {
  5. string connectionString = DataBaseApp.GetBusinessDbStringByLidarcode(lidarcode);
  6. using (IDbConnection connection = new SqlConnection(connectionString))
  7. {
  8. if (connection.State != ConnectionState.Open)
  9. {
  10. connection.Open();
  11. }
  12. string basesql = "INSERT INTO LidarPointDeformation(id, pointId, row, col, reffile, deformation, lidarcode, monitoringtime) VALUES";
  13. StringBuilder sb = new StringBuilder();
  14. int count = 0;
  15. for (int i = 0; i < list.Count; i++)
  16. {
  17. var item = list[i];
  18. string contractsql = String.Format(" ('{0}',{1},{2},{3},'{4}',{5},'{6}','{7}'),", item.id, item.pointId, item.row, item.col, item.reffile, item.deformation, item.lidarcode, item.monitoringtime);
  19. sb.Append(contractsql);
  20. if (i % 990 == 1)
  21. {
  22. sb.Remove(sb.Length - 1, 1);
  23. connection.Execute(basesql + sb.ToString());
  24. sb.Clear();
  25. continue;
  26. }
  27. else if (i == list.Count - 1)
  28. {
  29. sb.Remove(sb.Length - 1, 1);
  30. connection.Execute(basesql + sb.ToString());
  31. }
  32. else
  33. {
  34. continue;
  35. }
  36. }
  37. }
  38. }
  39. catch (Exception e)
  40. {
  41. throw;
  42. }
  43. }

第三、找了很久,说是用DapperExtensions,效率很不错

  1. /// <summary>
  2. /// 最后确定使用DapperExtensions,效率高很多
  3. /// </summary>
  4. /// <param name="list"></param>
  5. /// <param name="lidarcode"></param>
  6. public static void InsertByDapperExtension(List<Model.LidarPointDeformation> list, string lidarcode)
  7. {
  8. string connectionString = DataBaseApp.GetBusinessDbStringByLidarcode(lidarcode);
  9. using (IDbConnection connection = new SqlConnection(connectionString))
  10. {
  11. connection.Open();
  12. InsertBatch<Model.LidarPointDeformation>(connection, list);
  13. }
  14. }
  15. /// <summary>
  16. /// 批量插入
  17. /// </summary>
  18. public static void InsertBatch<T>(IDbConnection conn, IEnumerable<T> entityList, IDbTransaction transaction = null) where T : class
  19. {
  20. var tblName = string.Format("dbo.{0}", typeof(T).Name);
  21. var tran = (SqlTransaction)transaction;
  22. using (var bulkCopy = new SqlBulkCopy(conn as SqlConnection, SqlBulkCopyOptions.TableLock, tran))
  23. {
  24. try
  25. {
  26. bulkCopy.BatchSize = entityList.Count();
  27. bulkCopy.DestinationTableName = tblName;
  28. var table = new DataTable();
  29. DapperExtensions.Sql.ISqlGenerator sqlGenerator = new SqlGeneratorImpl(new DapperExtensionsConfiguration());
  30. var classMap = sqlGenerator.Configuration.GetMap<T>();
  31. var props = classMap.Properties.Where(x => x.Ignored == false).ToArray();
  32. foreach (var propertyInfo in props)
  33. {
  34. bulkCopy.ColumnMappings.Add(propertyInfo.Name, propertyInfo.Name);
  35. table.Columns.Add(propertyInfo.Name, Nullable.GetUnderlyingType(propertyInfo.PropertyInfo.PropertyType) ?? propertyInfo.PropertyInfo.PropertyType);
  36. }
  37. var values = new object[props.Count()];
  38. foreach (var itemm in entityList)
  39. {
  40. for (var i = 0; i < values.Length; i++)
  41. {
  42. values[i] = props[i].PropertyInfo.GetValue(itemm, null);
  43. }
  44. table.Rows.Add(values);
  45. }
  46. bulkCopy.WriteToServer(table);
  47. }
  48. catch (Exception e)
  49. {
  50. throw e;
  51. }
  52. }
  53. }

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号