您现在的位置是:首页 > 文章详情文章详情

C# SqlBulkCopy ,对大量数据新增操作

会飞的猪 2017-12-28 895人已围观

简介白码驿站是一个面向开发者的知识共享社区,专注于为开发者打造一个纯净的技术交流社区(源码下载,免费在线图片压缩,jquery插件,插件下载,JS/CSS在线压缩。)-白码驿站

    前些日期公司需要将一个excel中的社保数据导入到数据库, 大约四千多条数据吧,字段差不多60多个的样子, 刚开始用的是insert的语句, 发现速度很慢, 而且动不动就超时了, 导致数据新增失败。后来试着拼接一次插入多条的sql语句进行插入, 结果报错了,大概的意思是, 批量插入的数量超过了最大值。至此批量插入宣告失败。

    经过几番周折,选择使用SqlBulkCopy 方法进行批量插入。SqlBulkCopy 也有它的弊端, 那就是插入语句的时候不等进行查询, 例如:我们用insert操作的时候 , 是可以去数据库查询数据是否存在, 如果存在就不插入,但是SqlBulkCopy 插入数据时是不能进行查询的。 (我一次插入四千多条数据, 还不到一秒钟)

下面就来看看如何使用SqlBulkCopy 方法吧

                                        
        /// <summary>
        /// 快速插入数据
        /// </summary>
        /// <param name="dt">数据源</param>
        public void DoSqlBulkCopy(DataTable dt)
        {
            using (SqlConnection conn = new SqlConnection(strConn))
            {
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                using (System.Data.SqlClient.SqlBulkCopy sqlBC = new System.Data.SqlClient.SqlBulkCopy(conn))
                {
                    //判断时候有数据
                    if (dt.Rows.Count <= 0)
                    {
                        return;
                    }
                    sqlBC.BatchSize = 1000;//每1000条数据一个事物
                    sqlBC.BulkCopyTimeout = 300;//超时时间
                    sqlBC.DestinationTableName = "dbo.xhs_SocialSecurity";//表名
                    sqlBC.ColumnMappings.Add("F1", "name");
                    sqlBC.ColumnMappings.Add("F2", "IDNumber");
                    sqlBC.ColumnMappings.Add("F3", "yl_jishu");
                    sqlBC.ColumnMappings.Add("F4", "yl_company_bili");
                    sqlBC.ColumnMappings.Add("F5", "yl_company_yingjiao");
                    sqlBC.ColumnMappings.Add("F6", "yl_company_bujiao");
                    sqlBC.ColumnMappings.Add("F7", "yl_oneself_bili");
                    sqlBC.ColumnMappings.Add("F8", "yl_oneself_yingjiao");
                    sqlBC.ColumnMappings.Add("F9", "yl_oneself_bujiao");
                    sqlBC.ColumnMappings.Add("F10", "yiLiao_jishu");
                    sqlBC.ColumnMappings.Add("F11", "yiLiao_company_bili");
                    sqlBC.ColumnMappings.Add("F12", "yiLiao_company_guding");
                    sqlBC.ColumnMappings.Add("F13", "yiLiao_company_yingjiao");
                    sqlBC.ColumnMappings.Add("F14", "yiLiao_company_bujiao");
                    sqlBC.ColumnMappings.Add("F15", "yiLiao_oneself_bili");
                    sqlBC.ColumnMappings.Add("F16", "yiLiao_oneself_guding");
                    sqlBC.ColumnMappings.Add("F17", "yiLiao_oneself_yingjiao");
                    sqlBC.ColumnMappings.Add("F18", "yiLiao_oneself_bujiao");
                    sqlBC.ColumnMappings.Add("F19", "sy_jishu");
                    sqlBC.ColumnMappings.Add("F20", "sy_company_bili");
                    sqlBC.ColumnMappings.Add("F21", "sy_company_yingjiao");
                    sqlBC.ColumnMappings.Add("F22", "sy_company_bujiao");
                    sqlBC.ColumnMappings.Add("F23", "sy_oneself_bili");
                    sqlBC.ColumnMappings.Add("F24", "sy_oneself_yingjiao");
                    sqlBC.ColumnMappings.Add("F25", "sy_oneself_bujiao");
                    sqlBC.ColumnMappings.Add("F26", "gjj_jishu");
                    sqlBC.ColumnMappings.Add("F27", "gjj_company_bili");
                    sqlBC.ColumnMappings.Add("F28", "gjj_company_yingjiao");
                    sqlBC.ColumnMappings.Add("F29", "gjj_company_bujiao");
                    sqlBC.ColumnMappings.Add("F30", "gjj_oneself_bili");
                    sqlBC.ColumnMappings.Add("F31", "gjj_oneself_yingjiao");
                    sqlBC.ColumnMappings.Add("F32", "gjj_oneself_bujiao");
                    sqlBC.ColumnMappings.Add("F33", "qt_company_yingjiao");
                    sqlBC.ColumnMappings.Add("F34", "qt_company_bujiao");
                    sqlBC.ColumnMappings.Add("F35", "qt_oneself_yingjiao");
                    sqlBC.ColumnMappings.Add("F36", "qt_oneself_bujiao");
                    sqlBC.ColumnMappings.Add("F37", "whsbznj");
                    sqlBC.ColumnMappings.Add("F38", "whybznj");
                    sqlBC.ColumnMappings.Add("F39", "shengyu_jishu");
                    sqlBC.ColumnMappings.Add("F40", "shengyu_company_bili");
                    sqlBC.ColumnMappings.Add("F41", "shengyu_company_yingjiao");
                    sqlBC.ColumnMappings.Add("F42", "shengyu_company_bujiao");
                    sqlBC.ColumnMappings.Add("F43", "shengyu_oneself_bili");
                    sqlBC.ColumnMappings.Add("F44", "shengyu_oneself_yingjiao");
                    sqlBC.ColumnMappings.Add("F45", "shengyu_oneself_bujiao");
                    sqlBC.ColumnMappings.Add("F46", "gs_jishu");
                    sqlBC.ColumnMappings.Add("F47", "gs_company_bili");
                    sqlBC.ColumnMappings.Add("F48", "gs_company_yingjiao");
                    sqlBC.ColumnMappings.Add("F49", "gs_company_bujiao");
                    sqlBC.ColumnMappings.Add("F50", "gs_oneself_bili");
                    sqlBC.ColumnMappings.Add("F51", "gs_oneself_yingjiao");
                    sqlBC.ColumnMappings.Add("F52", "gs_oneself_bujiao");
                    sqlBC.ColumnMappings.Add("F53", "serviceCharge");
                    sqlBC.ColumnMappings.Add("F54", "totalCost");
                    sqlBC.ColumnMappings.Add("F55", "grossWage");
                    sqlBC.ColumnMappings.Add("F56", "payTaxes");
                    sqlBC.ColumnMappings.Add("F57", "yearEendBonus");
                    sqlBC.ColumnMappings.Add("F58", "yearEendBonusTax");
                    sqlBC.ColumnMappings.Add("F59", "releasePay");
                    sqlBC.ColumnMappings.Add("F60", "releasePayTax");
                    sqlBC.ColumnMappings.Add("F61", "financeYear");
                    
                    sqlBC.WriteToServer(dt);

                }
            }
        }
                                

上面 sqlBC.ColumnMappings.Add("数据源列名称","数据库表字段名");


不过这样并没有完, 要注意 数据源列名称的类型要与数据库表字段列类型一致,来看看具体怎么解决吧,

我将excel中的数据读到一个DataTable中, 所以这个DataTable的每一列的类型都应该是string类型。但是我们插入数据时,有些字段并不是string类型,这回导致出错,

所以我们在插入数据时,DataTable数据源的每一列的类型应该与数据库的一致,


创建一个新的DataTable,列类型与数据库一致

                                        
                    
                    DataTable newDt = new DataTable();
                    newDt.Columns.Add("F1", Type.GetType("System.String"));
                    newDt.Columns.Add("F2", Type.GetType("System.String"));
                    newDt.Columns.Add("F3", Type.GetType("System.Double"));
                    newDt.Columns.Add("F4", Type.GetType("System.Double"));
                    newDt.Columns.Add("F5", Type.GetType("System.Double"));
                    newDt.Columns.Add("F6", Type.GetType("System.Double"));
                    newDt.Columns.Add("F7", Type.GetType("System.Double"));
                    newDt.Columns.Add("F8", Type.GetType("System.Double"));
                    newDt.Columns.Add("F9", Type.GetType("System.Double"));
                    newDt.Columns.Add("F10", Type.GetType("System.Double"));
                    newDt.Columns.Add("F11", Type.GetType("System.Double"));
                    newDt.Columns.Add("F12", Type.GetType("System.Double"));
                    newDt.Columns.Add("F13", Type.GetType("System.Double"));
                    newDt.Columns.Add("F14", Type.GetType("System.Double"));
                    newDt.Columns.Add("F15", Type.GetType("System.Double"));
                    newDt.Columns.Add("F16", Type.GetType("System.Double"));
                    newDt.Columns.Add("F17", Type.GetType("System.Double"));
                    newDt.Columns.Add("F18", Type.GetType("System.Double"));
                    newDt.Columns.Add("F19", Type.GetType("System.Double"));
                    newDt.Columns.Add("F20", Type.GetType("System.Double"));
                    newDt.Columns.Add("F21", Type.GetType("System.Double"));
                    newDt.Columns.Add("F22", Type.GetType("System.Double"));
                    newDt.Columns.Add("F23", Type.GetType("System.Double"));
                    newDt.Columns.Add("F24", Type.GetType("System.Double"));
                    newDt.Columns.Add("F25", Type.GetType("System.Double"));
                    newDt.Columns.Add("F26", Type.GetType("System.Double"));
                    newDt.Columns.Add("F27", Type.GetType("System.Double"));
                    newDt.Columns.Add("F28", Type.GetType("System.Double"));
                    newDt.Columns.Add("F29", Type.GetType("System.Double"));
                    newDt.Columns.Add("F30", Type.GetType("System.Double"));
                    newDt.Columns.Add("F31", Type.GetType("System.Double"));
                    newDt.Columns.Add("F32", Type.GetType("System.Double"));
                    newDt.Columns.Add("F33", Type.GetType("System.Double"));
                    newDt.Columns.Add("F34", Type.GetType("System.Double"));
                    newDt.Columns.Add("F35", Type.GetType("System.Double"));
                    newDt.Columns.Add("F36", Type.GetType("System.Double"));
                    newDt.Columns.Add("F37", Type.GetType("System.Double"));
                    newDt.Columns.Add("F38", Type.GetType("System.Double"));
                    newDt.Columns.Add("F39", Type.GetType("System.Double"));
                    newDt.Columns.Add("F40", Type.GetType("System.Double"));
                    newDt.Columns.Add("F41", Type.GetType("System.Double"));
                    newDt.Columns.Add("F42", Type.GetType("System.Double"));
                    newDt.Columns.Add("F43", Type.GetType("System.Double"));
                    newDt.Columns.Add("F44", Type.GetType("System.Double"));
                    newDt.Columns.Add("F45", Type.GetType("System.Double"));
                    newDt.Columns.Add("F46", Type.GetType("System.Double"));
                    newDt.Columns.Add("F47", Type.GetType("System.Double"));
                    newDt.Columns.Add("F48", Type.GetType("System.Double"));
                    newDt.Columns.Add("F49", Type.GetType("System.Double"));
                    newDt.Columns.Add("F50", Type.GetType("System.Double"));
                    newDt.Columns.Add("F51", Type.GetType("System.Double"));
                    newDt.Columns.Add("F52", Type.GetType("System.Double"));
                    newDt.Columns.Add("F53", Type.GetType("System.Double"));
                    newDt.Columns.Add("F54", Type.GetType("System.Double"));
                    newDt.Columns.Add("F55", Type.GetType("System.Double"));
                    newDt.Columns.Add("F56", Type.GetType("System.Double"));
                    newDt.Columns.Add("F57", Type.GetType("System.Double"));
                    newDt.Columns.Add("F58", Type.GetType("System.Double"));
                    newDt.Columns.Add("F59", Type.GetType("System.Double"));
                    newDt.Columns.Add("F60", Type.GetType("System.Double"));
                    newDt.Columns.Add("F61", Type.GetType("System.String"));
                                


将数据源的值复制到新的DataTable中

                                        
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        try
                        {
                            DataRow row = newDt.NewRow();
                            row["F1"] = dt.Rows[i]["F1"];
                            row["F2"] = dt.Rows[i]["F2"];
                            row["F3"] = Convert.ToDouble(dt.Rows[i]["F3"]);
                            row["F4"] = Convert.ToDouble(dt.Rows[i]["F4"]);
                            row["F5"] = Convert.ToDouble(dt.Rows[i]["F5"]);
                            row["F6"] = Convert.ToDouble(dt.Rows[i]["F6"]);
                            row["F7"] = Convert.ToDouble(dt.Rows[i]["F7"]);
                            row["F8"] = Convert.ToDouble(dt.Rows[i]["F8"]);
                            row["F9"] = Convert.ToDouble(dt.Rows[i]["F9"]);
                            row["F10"] = Convert.ToDouble(dt.Rows[i]["F10"]);
                            row["F11"] = Convert.ToDouble(dt.Rows[i]["F11"]);
                            row["F12"] = Convert.ToDouble(dt.Rows[i]["F12"]);
                            row["F13"] = Convert.ToDouble(dt.Rows[i]["F13"]);
                            row["F14"] = Convert.ToDouble(dt.Rows[i]["F14"]);
                            row["F15"] = Convert.ToDouble(dt.Rows[i]["F15"]);
                            row["F16"] = Convert.ToDouble(dt.Rows[i]["F16"]);
                            row["F17"] = Convert.ToDouble(dt.Rows[i]["F17"]);
                            row["F18"] = Convert.ToDouble(dt.Rows[i]["F18"]);
                            row["F19"] = Convert.ToDouble(dt.Rows[i]["F19"]);
                            row["F20"] = Convert.ToDouble(dt.Rows[i]["F20"]);
                            row["F21"] = Convert.ToDouble(dt.Rows[i]["F21"]);
                            row["F22"] = Convert.ToDouble(dt.Rows[i]["F22"]);
                            row["F23"] = Convert.ToDouble(dt.Rows[i]["F23"]);
                            row["F24"] = Convert.ToDouble(dt.Rows[i]["F24"]);
                            row["F25"] = Convert.ToDouble(dt.Rows[i]["F25"]);
                            row["F26"] = Convert.ToDouble(dt.Rows[i]["F26"]);
                            row["F27"] = Convert.ToDouble(dt.Rows[i]["F27"]);
                            row["F28"] = Convert.ToDouble(dt.Rows[i]["F28"]);
                            row["F29"] = Convert.ToDouble(dt.Rows[i]["F29"]);
                            row["F30"] = Convert.ToDouble(dt.Rows[i]["F30"]);
                            row["F31"] = Convert.ToDouble(dt.Rows[i]["F31"]);
                            row["F32"] = Convert.ToDouble(dt.Rows[i]["F32"]);
                            row["F33"] = Convert.ToDouble(dt.Rows[i]["F33"]);
                            row["F34"] = Convert.ToDouble(dt.Rows[i]["F34"]);
                            row["F35"] = Convert.ToDouble(dt.Rows[i]["F35"]);
                            row["F36"] = Convert.ToDouble(dt.Rows[i]["F36"]);
                            row["F37"] = Convert.ToDouble(dt.Rows[i]["F37"]);
                            row["F38"] = Convert.ToDouble(dt.Rows[i]["F38"]);
                            row["F39"] = Convert.ToDouble(dt.Rows[i]["F39"]);
                            row["F40"] = Convert.ToDouble(dt.Rows[i]["F40"]);
                            row["F41"] = Convert.ToDouble(dt.Rows[i]["F41"]);
                            row["F42"] = Convert.ToDouble(dt.Rows[i]["F42"]);
                            row["F43"] = Convert.ToDouble(dt.Rows[i]["F43"]);
                            row["F44"] = Convert.ToDouble(dt.Rows[i]["F44"]);
                            row["F45"] = Convert.ToDouble(dt.Rows[i]["F45"]);
                            row["F46"] = Convert.ToDouble(dt.Rows[i]["F46"]);
                            row["F47"] = Convert.ToDouble(dt.Rows[i]["F47"]);
                            row["F48"] = Convert.ToDouble(dt.Rows[i]["F48"]);
                            row["F49"] = Convert.ToDouble(dt.Rows[i]["F49"]);
                            row["F50"] = Convert.ToDouble(dt.Rows[i]["F50"]);
                            row["F51"] = Convert.ToDouble(dt.Rows[i]["F51"]);
                            row["F52"] = Convert.ToDouble(dt.Rows[i]["F52"]);
                            row["F53"] = Convert.ToDouble(dt.Rows[i]["F53"]);
                            row["F54"] = Convert.ToDouble(dt.Rows[i]["F54"]);
                            row["F55"] = Convert.ToDouble(dt.Rows[i]["F55"]);
                            row["F56"] = Convert.ToDouble(dt.Rows[i]["F56"]);
                            row["F57"] = Convert.ToDouble(dt.Rows[i]["F57"]);
                            row["F58"] = Convert.ToDouble(dt.Rows[i]["F58"]);
                            row["F59"] = Convert.ToDouble(dt.Rows[i]["F59"]);
                            row["F60"] = Convert.ToDouble(dt.Rows[i]["F60"]);
                            row["F61"] = dt.Rows[i]["F61"];
                            newDt.Rows.Add(row);
                        }
                        catch (Exception e )
                        {

                            string s = e.Message;
                        }
                        
                    }
                                

这样就成功了, 就是这么简单,,,

Tags: C#

很赞哦! (0)

评 论

2018-01-09 15:55:46

点击排行

系统处理中...