• 从文本中读入数据并插入到数据库,测试数据量为6百多万条.


                string path = @"D:\1.sql";
                StreamReader sr = File.OpenText(path);
                DataTable dt = new DataTable();
                dt.Columns.Add("UserName",typeof(string));
                dt.Columns.Add("UserPwd", typeof(string));
                dt.Columns.Add("Email", typeof(string));
                int i = 0;
                int j = 0;
                while (!sr.EndOfStream)
                {
                    string str = sr.ReadLine().Trim();
                    if(str == "")
                    {
                        continue;
                    }
                    string[] arr = StringHelper.SplitString(str, " # ");
                    try
                    {
                        if (arr.Length <= 1)
                            continue;
                        DataRow dr = dt.NewRow();
                        dr[0] = arr[0].Trim();
                        dr[1] = arr[1].Trim();
                        dr[2] = arr[2].Trim();
                        dt.Rows.Add(dr);
                    }
                    catch
                    {
                        continue;
                    }

                    i++;
                    j++;
                    if (dt.Rows.Count == 4000)
                    {
                        SqlBulkCopy bulkCopy1 = new SqlBulkCopy(ConfigurationSettings.AppSettings["SqlConnString"].ToString(), SqlBulkCopyOptions.Default);
                        //每一批次中的行数
                        bulkCopy1.BatchSize = 1000;
                        bulkCopy1.DestinationTableName = "Users";
                        SqlBulkCopyColumnMapping mapping1 = new SqlBulkCopyColumnMapping("UserName", "UserName");
                        SqlBulkCopyColumnMapping mapping2 = new SqlBulkCopyColumnMapping("UserPwd", "UserPwd");
                        SqlBulkCopyColumnMapping mapping3 = new SqlBulkCopyColumnMapping("Email", "Email");
                        bulkCopy1.ColumnMappings.Add(mapping1);
                        bulkCopy1.ColumnMappings.Add(mapping2);
                        bulkCopy1.ColumnMappings.Add(mapping3);
                        bulkCopy1.WriteToServer(dt);
                        bulkCopy1.Close();
                        dt.Rows.Clear();
                        i = 0;
                    }
                   
                }
                if (dt.Rows.Count > 0)
                {
                    SqlBulkCopy bulkCopy = new SqlBulkCopy(ConfigurationSettings.AppSettings["SqlConnString"].ToString(), SqlBulkCopyOptions.Default);
                    //每一批次中的行数
                    bulkCopy.BatchSize = 1000;
                    bulkCopy.DestinationTableName = "Users";
                    SqlBulkCopyColumnMapping mapping1 = new SqlBulkCopyColumnMapping("UserName", "UserName");
                    SqlBulkCopyColumnMapping mapping2 = new SqlBulkCopyColumnMapping("UserPwd", "UserPwd");
                    SqlBulkCopyColumnMapping mapping3 = new SqlBulkCopyColumnMapping("Email", "Email");
                    bulkCopy.ColumnMappings.Add(mapping1);
                    bulkCopy.ColumnMappings.Add(mapping2);
                    bulkCopy.ColumnMappings.Add(mapping3);
                    bulkCopy.WriteToServer(dt);
                    bulkCopy.Close();
                    dt.Rows.Clear();
                }

  • 相关阅读:
    Codeforces Round #534 Div. 1
    Codeforces Round #540 Div. 3 F2
    Educational Codeforces Round 60 Div. 2
    Luogu4389 付公主的背包(生成函数+多项式exp)
    BZOJ1005 HNOI2008明明的烦恼(prufer+高精度)
    Codeforces Round #539 Div. 1
    js --- 关于DOM的事件操作
    js函数
    js常用内置对象
    js数据类型转换 ----流程控制
  • 原文地址:https://www.cnblogs.com/059212315/p/2297509.html
Copyright © 2020-2023  润新知