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;
            }
        }

 

转载于:https://www.cnblogs.com/daniel-niu/p/10682785.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值