国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

SqlBulkCopy高效能批量插入SQLSERVER

2019-11-14 16:04:17
字體:
供稿:網(wǎng)友

what

SqlBulkCopy是.NET提供的用來批量插入數(shù)據(jù)的一個(gè)類,特別是將內(nèi)存中的數(shù)據(jù)一次性插入到數(shù)據(jù)庫,目前只能插入到SQL SERVER數(shù)據(jù)庫,數(shù)據(jù)源可以是DataTable、IDataReader

why

SqlBulkCopy插入與循環(huán)一條條插入相比,性能有巨大提升,數(shù)據(jù)越多,性能優(yōu)勢(shì)越明顯。

測(cè)試結(jié)果:一萬條數(shù)據(jù),一條條插入要6秒,Bulk只需要0.1秒。理論上插入百萬條記錄也只需要1分鐘以內(nèi)

how

以下是測(cè)試代碼:

       string sourceConStr = "Data Source=192.168.1.100;Initial Catalog=A; uid=sa;pwd=sa";        string destConStr = "Data Source=.;Initial Catalog=B; uid=sa;pwd=sa";        DataTable dt = new DataTable();        public Form1()        {            InitializeComponent();        }        PRivate void button3_Click(object sender, EventArgs e)        {            dt = GetTable();            MessageBox.Show("get finish");        }        private void button1_Click(object sender, EventArgs e)        {            string str = "queue start...!  /n";            Stopwatch sw = new Stopwatch();            sw.Start();            CopyData(dt);            sw.Stop();            str += "queue cost time is " + sw.ElapsedMilliseconds + "/n";            richTextBox1.Text = str;        }        private void button2_Click(object sender, EventArgs e)        {            string str = "bulk start...!  /n";            Stopwatch sw = new Stopwatch();            sw.Start();            CopyDataBulk(dt);            sw.Stop();            str += "bulk cost time is " + sw.ElapsedMilliseconds + "/n";            richTextBox2.Text = str;        }        //從數(shù)據(jù)源獲取要插入的數(shù)據(jù)        private DataTable GetTable()        {            DataTable dt = new DataTable();            using (SqlConnection sourceConnection = new SqlConnection(sourceConStr))            {                sourceConnection.Open();                SqlCommand cmd = new SqlCommand("SELECT TOP 10000 CName,PersonID,Sex,Age FROM Customer order by cid asc;", sourceConnection);                cmd.CommandTimeout = 600000;                SqlDataAdapter da = new SqlDataAdapter(cmd);                da.Fill(dt);            }            return dt;        }        //一條條插入        private void CopyData(DataTable dt)        {            using (SqlConnection destinationConnection = new SqlConnection(destConStr))            {                destinationConnection.Open();                foreach(DataRow reader in dt.Rows)                {                     string sql = "INSERT INTO Customer(Name,PersonID,Sex,Age) VALUES('" + reader["Cname"].ToString() + "','" + reader["PersonID"].ToString() + "','" + reader["Sex"].ToString() + "','" + reader["Age"].ToString() + "')";                    SqlCommand cmd = new SqlCommand(sql, destinationConnection);                    try                    {                        int re = cmd.ExecuteNonQuery();                    }                    catch (Exception ex)                    {                        MessageBox.Show(ex.Message);                    }                }            }        }        //Bulk插入        private void CopyDataBulk(DataTable dt)        {            using (SqlConnection destinationConnection = new SqlConnection(destConStr))            {                destinationConnection.Open();                using (SqlBulkCopy bulkCopy = new SqlBulkCopy(destinationConnection))                {                    //寫對(duì)應(yīng)關(guān)系。如舊表的CName列的數(shù)據(jù),對(duì)應(yīng)新表Name列                     bulkCopy.ColumnMappings.Add("CName", "Name");                    //設(shè)置目標(biāo)表名                    bulkCopy.DestinationTableName = "Customer";                    try                    {                        bulkCopy.WriteToServer(dt);                    }                    catch (Exception ex)                    {                        MessageBox.Show(ex.Message);                    }                    finally                    {                       // reader.Close();                    }                }            }        }

 


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 库尔勒市| 苍山县| 巩义市| 津南区| 华池县| 安乡县| 北川| 宁国市| 略阳县| 凤台县| 南澳县| 宁南县| 越西县| 连城县| 西乌珠穆沁旗| 连城县| 福海县| 蕉岭县| 金华市| 克拉玛依市| 平利县| 阿拉善左旗| 共和县| 门源| 普兰店市| 滨海县| 犍为县| 民乐县| 山东| 屏东县| 松桃| 西华县| 安丘市| 泗洪县| 南涧| 清丰县| 吉隆县| 镇赉县| 天峻县| 依兰县| 呼和浩特市|