zoukankan      html  css  js  c++  java
  • 快速读取TXT几百万行数据, 然后插入到数据,SqlBulkCopy功能的确是有效率

    public static void Main(string[] args)
            {
                string strPath = "F:\Download\600.txt";
                int lines = 1;
                Console.WriteLine("正在读取行数...."+DateTime.Now.ToString());
                DataTable dt = new DataTable();
                dt.Columns.Add("ID", System.Type.GetType("System.Int32"));
                dt.Columns.Add("UserName",System.Type.GetType("System.String"));            
                dt.Columns.Add("Password", System.Type.GetType("System.String"));
                dt.Columns.Add("Email", System.Type.GetType("System.String"));
    
                using (var sr = new StreamReader(strPath))
                {
                    var ls = "";
                    while ((ls = sr.ReadLine()) != null)
                    {
                        if (ls.Contains("#"))
                        {
                            string[] templine = ls.Split('#');
                            if (templine.Length == 3)
                            {
                                DataRow item = dt.NewRow();
                                item["ID"] = lines;
                                item["UserName"] = templine[0].Trim();
                                item["Password"] = templine[1].Trim();
                                item["Email"] = templine[2].Trim();
                                dt.Rows.Add(item);
                                lines++;
                            }
                        }
                    }
                }
    
                if (dt.Rows.Count > 0)
                {
                    string strConnection = "server=localhost;database=Data;uid=sa;pwd=123456;Connect Timeout=50000";
                    SqlBulkCopyInsert(strConnection, "tb_csdn_data", dt);
                }
    
                Console.WriteLine("结束读取行数" + dt.Rows.Count + "...." + DateTime.Now.ToString());
                Console.ReadLine();
            }
    
            public static void SqlBulkCopyInsert(string strConnection, string strTableName, DataTable dt)
            {
                try
                {
                    using (SqlBulkCopy sqlRevdBulkCopy = new SqlBulkCopy(strConnection))
                    {
                        sqlRevdBulkCopy.BulkCopyTimeout = 50000;
                        sqlRevdBulkCopy.DestinationTableName = strTableName; 
                        sqlRevdBulkCopy.NotifyAfter = dt.Rows.Count;
                        sqlRevdBulkCopy.WriteToServer(dt);
                        sqlRevdBulkCopy.Close(); 
                    }
                }
                catch (Exception ex)
                {
                    throw (ex);
                }
            }
    

      

  • 相关阅读:
    ThinkPHP讲解(一)框架基础
    smarty简单介绍
    留言板
    文件系统处理
    文件上传(带有预览模式)
    文件上传(无预览模式版)
    注册、登陆、审核练习
    session讲解(二)——商城购物车练习
    session讲解(一)——登录网页练习
    P6216 回文匹配
  • 原文地址:https://www.cnblogs.com/whtydn/p/11377716.html
Copyright © 2011-2022 走看看