• C# 利用 SqlBulkCopy 大批量导入excel文件进入SQL


       public void BuckCopy(DataTable dt, string destinationTableName)
            {
                using (SqlConnection sqlConn = (SqlConnection)this.CurrentDataBase.CreateConnection())
                {
                    if (sqlConn.State != ConnectionState.Open)
                        sqlConn.Open();
                    using (SqlBulkCopy buckCopy = new SqlBulkCopy(sqlConn))
                    {
                        buckCopy.DestinationTableName = destinationTableName;
                        buckCopy.ColumnMappings.Add("ID", "ID");                    
                        buckCopy.WriteToServer(dt);
                    }
                }
            }

     升级,动态mapping

      /// <summary>
            /// 将datatable数据bulkcopy进SQL table
            /// </summary>
            /// <param name="dt"></param>
            /// <param name="tablename"></param>
            /// <param name="_constring"></param>
            public static void SqlBulkCopy(DataTable dt, string tablename, string _constring)
            {
                try
                {
                    using (SqlConnection sqlConn = new SqlConnection(_constring))
                    {
                        if (sqlConn.State != ConnectionState.Open)
                            sqlConn.Open();
                        /*查询数据 tablename对应的字段*/
                        string[] restrictions = new string[4];
                        restrictions[1] = "dbo";
                        restrictions[2] = tablename;
                        /*获取table的列*/
                        DataTable tableinfo = sqlConn.GetSchema("Columns", restrictions);
                        if (tableinfo.Rows.Count > 0)
                        {
                            using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConn))
                            {
                                bulkCopy.BulkCopyTimeout = _timeout;
                                bulkCopy.DestinationTableName = tablename;
                                bulkCopy.BatchSize = 1000;//每1000条提交一次
                                foreach (DataColumn dc in dt.Columns)
                                {
                                    foreach (DataRow dr in tableinfo.Rows)
                                    {
                                        //动态mapping字段,大小写可以忽略
                                        if (dc.ColumnName.ToLower() == dr["COLUMN_NAME"].ToString().ToLower())
                                        {
                                            bulkCopy.ColumnMappings.Add(dc.ColumnName, dr["COLUMN_NAME"].ToString());
                                            break;//只要匹配到就跳出该次循环,进行下一次
                                        }
                                    }
                                }
                                bulkCopy.WriteToServer(dt);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Error("Error:Connercthelper SqlBulkCopy", ex);
                    throw;
                }
    
    
    
            }

    测试6W条数据,大概9-10秒。如果mapping是写死的,应该要快一些。

    又重新重载了一下方法:支持调用的时候先mapping好。

            /// <summary>
            ///  将datatable数据bulkcopy进SQL table
            /// </summary>
            /// <param name="dt"></param>
            /// <param name="tablename"></param>
            /// <param name="_constring"></param>
            /// <param name="columnmapping">自定义mapping column</param>
            public static void SqlBulkCopy(DataTable dt, string tablename, string _constring, SqlBulkCopyColumnMapping[] columnmapping)
            {
                try
                {
                      using (SqlConnection sqlConn = new SqlConnection(_constring))
                      {
                          if (sqlConn.State != ConnectionState.Open)
                              sqlConn.Open();
                           using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConn))
                           {
                               bulkCopy.BulkCopyTimeout = _timeout;
                               bulkCopy.DestinationTableName = tablename;
                               bulkCopy.BatchSize = 1000;//每1000条提交一次
                               //循环mapping
                               foreach(SqlBulkCopyColumnMapping map in columnmapping)
                               {
                                   bulkCopy.ColumnMappings.Add(map);
                               }
                               bulkCopy.WriteToServer(dt);
                           }
                      }
                }
                catch (Exception ex)
                {
                    LogHelper.Error("Error:Connercthelper SqlBulkCopy", ex);
                    throw;
                }
            }
  • 相关阅读:
    数1的个数
    找水王2
    书店促销
    返回一个二维整数数组中最大联通子数组的和
    敏捷软件开发读书笔记(三)
    软件工程团队开发——第一次冲刺会议总结
    返回一个二维整数数组中最大联通子数组的和
    结对项目开发电梯调度
    《最后期限》——读书笔记03
    最后期限——阅读笔记2
  • 原文地址:https://www.cnblogs.com/daniel-niu/p/10682785.html
Copyright © 2020-2023  润新知