大批量数据的插入之终极性能提升-SqlBulkCopy

  相信你有遇到过将数据从文本文件导入到数据库中,这样的情况吧 如果数据量比较大的话 用平时的sql语句(insert into 表名 values(。。。))导入的时候 是不是等得蛋疼啊

不管你有没有,反正今天我是遇到了,特此 记下解决方法:

  在System.Data.SqlClient这个命名空间下 有一个SqlBulkCopy类,我们用的就是它了

下面有实现的源码(实现的是将电话归属地导入到数据库中的实例)

  string sourceFileName="D:\\电话归属地.txt";//文件路径

   if(File.Exists(sourceFileName))
            {
                IEnumerableallLine=File.ReadAllLines(sourceFileName,Encoding.Default);
                using (SqlConnection conn = new SqlConnection("server=.;database=Test;Integrated Security=sspi;"))
                {
                    DateTime dtStart = DateTime.Now; 

using (SqlBulkCopy sqlBulkCopy=new SqlBulkCopy(conn))    

                 {                 

        //设置服务器上表的名称        

                 sqlBulkCopy.DestinationTableName = "Phone";        

                 conn.Open();             

            //SqlTransaction tran = conn.BeginTransaction();      

                   ///在内存中创建一个表 并为表添加列             

            DataTable dt = new DataTable();      

                   dt.Columns.Add("ID");          

               dt.Columns.Add("AreaName");         

                dt.Columns.Add("NumbType");        

                 dt.Columns.Add("AreaNumb");          

               try                         {               

              foreach (string item in allLine)             

                {                           

      string[] data = item.Split(new char[] { '\t', ' ' }, StringSplitOptions.RemoveEmptyEntries);      

                           int id = Convert.ToInt32(data[0]);         

                        string area = data[1].Trim('"');          

                       string numbType = data[2].Trim('"');         

                        string areaNumb = data[3].Trim('"');         

                        //创建一行 DataRow dr = dt.NewRow();    

                             dr["ID"] = id;           

                      dr["AreaName"] = area;           

                      dr["NumbType"] = numbType;          

                       dr["AreaNumb"] = areaNumb;         

                        //将该行添加到表中                                 dt.Rows.Add(dr);    

                         }                       

      //将类存中标的列与数据库中表的列映射                             sqlBulkCopy.ColumnMappings.Add("ID", "ID");                             sqlBulkCopy.ColumnMappings.Add("AreaName", "AreaName");          

                   sqlBulkCopy.ColumnMappings.Add("NumbType", "NumbType");        

                     sqlBulkCopy.ColumnMappings.Add("AreaNumb", "AreaNumb");         

                    //将内存中的表的数据写入到数据库表中                             sqlBulkCopy.WriteToServer(dt);    

                         TimeSpan ts=DateTime.Now-dtStart;    

                         MessageBox.Show(string.Format("导入成功!费时:{0}秒",ts.Seconds));        

                 }                         catch{MessageBox.Show("导入失败"); }

                    }

                  }             }

转载于:https://www.cnblogs.com/xiexingen/archive/2013/01/07/2850105.html

你可能感兴趣的:(数据库)