SqlBulkCopy实现大容量数据快速插入数据库中

一般情况下,我们手写sqlhelper类,在此类中定义一个数据插入到数据库的一个方法。将数据库连接密封在using()的语句中。using显示了Idispose接口。可以及时释放数据库连接资源。代码如下:

//以下为方法中语句

using(Sqlconnection conn=new Sqlconnection(constr))//"constr"为连接字符串

{

     conn.Open();

     using(SqlCommand cmd=conn.CreateCommand())

     {

          cmd.CommandText=sql;//"sql"为插入数据库操作语句

          cmd.ExecuteNonquery();

     }

}

但是这样的数据插入操作,面对大批量的数据插入时,所耗费的时间很长。有没有更好的方法能够实现数据的快速插入呢?

此时,微软早已为我们准备好了一个方法,可以实现数据的快速插入。

代码(我的项目的部分代码)如下:

      string constr = "Data Source=.;Initial Catalog=ceshi;Integrated Security=true";
            using(SqlConnection conn=new SqlConnection(constr))
            {
                conn.Open();
                DateTime starttime = System.DateTime.Now;
                using(SqlCommand cmd=conn.CreateCommand())
                {
                    OpenFileDialog ofd = new OpenFileDialog();
                    ofd.Filter = "文本文件|*.txt";
                    if (ofd.ShowDialog() == false)
                    {
                        return;
                    }
                    string filename = ofd.FileName;
                    string[] lines = File.ReadLines(filename,Encoding.Default).ToArray();
                    DataTable table = new DataTable();
                    table.Columns.Add("tel_id");
                    table.Columns.Add("area");
                    table.Columns.Add("tel_type");
                    table.Columns.Add("area_tel");
                    for (int i = 1; i < lines.Count(); i++)
                    {
                        string line=lines[i];
                        string[] liness = line.Split('\t');
                        string tel_id = liness[0];
                        string area=liness[1];
                        string tel_type=liness[2];
                        string area_tel = liness[3];
                        DataRow row = table.NewRow();
                        row["tel_id"]=tel_id;
                        row["area"] = area;
                        row["tel_type"] = tel_type;
                        row["area_tel"] = area_tel;
                        table.Rows.Add(row);
                    }
                    using(SqlBulkCopy sbc=new SqlBulkCopy(constr))//SqlBulkCopy是大批量数据复制
                    {

                          //以下括号中的第二个参数是数据库中表的字段名
                        sbc.DestinationTableName = "Tel";
                        sbc.ColumnMappings.Add("tel_id","tel_id");
                        sbc.ColumnMappings.Add("area", "area");
                        sbc.ColumnMappings.Add("tel_type", "tel_type");
                        sbc.ColumnMappings.Add("area_tel", "area_tel");
                        sbc.WriteToServer(table);
                    }
                    DateTime endtime = System.DateTime.Now;
                    TimeSpan ts = endtime - starttime;
                    double hao = ts.TotalSeconds;
                    MessageBox.Show(hao.ToString());
                }
            }

有兴趣的朋友可以试一下哦!

posted @ 2013-10-02 00:04  梦想的火把永不熄灭  阅读(307)  评论(0编辑  收藏  举报