当前位置:   article > 正文

批量添加数据的方法_columnmappings.add

columnmappings.add

在一些大量数据导入数据库的情况下使用,性能提高很大,需要注意几点

一:方法的参数  是一个datatable。这个表的字段结构必须要实际库中表的结构一致,否则添加失败。

二:需要指定表名。tablename就是表名

 

/// <summary>
        /// 批量插入文件流水数据
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public int BulkCopyDataToDB(DataTable dtItems)
        {
            try
            {
                string tableName = "LV_OrderList";
                //插入原始数据到数据库中
                using (SqlBulkCopy bulkCopy = new SqlBulkCopy(ConfigurationManager.ConnectionStrings["YekCMS"].ToString()))
                {
                    bulkCopy.DestinationTableName = tableName;//要插入的表的表名
                    bulkCopy.ColumnMappings.Add("orderId", "[orderId]");
                    if (dtItems.Columns.Contains("arg1"))
                    {
                        bulkCopy.ColumnMappings.Add("arg1", "[arg1]");
                    }
                    if (dtItems.Columns.Contains("arg2"))
                    {
                        bulkCopy.ColumnMappings.Add("arg2", "[arg2]");
                    }
                    bulkCopy.ColumnMappings.Add("categoryName", "[categoryName]");
                    bulkCopy.ColumnMappings.Add("productId", "[productId]");
                    bulkCopy.ColumnMappings.Add("productName", "[productName]");
                    bulkCopy.ColumnMappings.Add("oughtAmount", "[oughtAmount]");
                    bulkCopy.ColumnMappings.Add("paymentStatus", "[paymentStatus]");
                    bulkCopy.ColumnMappings.Add("productType", "[productType]");
                    bulkCopy.ColumnMappings.Add("buName", "[buCode]");
                    bulkCopy.ColumnMappings.Add("orderStatus", "[orderStatus]");
                    bulkCopy.ColumnMappings.Add("mediaId", "[mediaId]");
                    bulkCopy.ColumnMappings.Add("comm_type", "[comm_type]");
                    bulkCopy.ColumnMappings.Add("comm_status", "[comm_status]");
                    bulkCopy.ColumnMappings.Add("comm_rate", "[comm_rate]");
                    bulkCopy.ColumnMappings.Add("commission", "[commission]");
                    bulkCopy.ColumnMappings.Add("order_amout", "[order_amout]");
                    bulkCopy.ColumnMappings.Add("balance_status", "[balance_status]");
                    bulkCopy.ColumnMappings.Add("distribution_source", "[distribution_source]");
                    bulkCopy.ColumnMappings.Add("createDate", "[createDate]");
                    bulkCopy.BulkCopyTimeout = 600;
                    bulkCopy.BatchSize = 10000;
                    bulkCopy.WriteToServer(dtItems);
                }

                return 1;
            }
            catch 
            {
                return 0;
            }
        }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/511891
推荐阅读
相关标签
  

闽ICP备14008679号