ADO.NET下的SqlBulkCopy類執行資料庫間批量複製操作

來源:互聯網
上載者:User

 

很多時侯,我們需要在資料庫間複製大量資料,如SQLSERVER提供的BCP.EXE命令列工具,如ADO.NET下

 

SqlBulkCopy類提供了一個高效能的方法WriteToServer來複製資料。該方法可以接受一個DataRow對像資料、一

 

個實現IDbDataReader介面的對像和一個DataTable,或者接受一個DataTable對像和一個DataRowState枚舉值,

 

以實現從大多數位置中擷取資料。

 

        

 

        /// <summary>
        /// 在目標資料庫與來源資料庫之間批量複製資料
        /// </summary>
        /// <param name="SourceConnectionstr">來源資料庫連接字串</param>
        /// <param name="TargetConnectionstr">目標資料庫連接字串</param>
        /// <param name="SourceSQLstr">查詢資料SQL語句,必須包含主鍵</param>
        /// <param name="TargetTableName">更新目標資料庫中的表名</param>
        /// <returns>Boolean</returns>
       

        public static Boolean dbtodb(string SourceConnectionstr,string TargetConnectionstr,

                                     string SourceSQLstr,string TargetTableName)
        {
            System.Data.SqlClient.SqlConnection
SourceConn= new 

                                                           
System.Data.SqlClient.SqlConnection();
            System.Data.SqlClient.SqlConnection
TargetConn = new                  

                                                           
System.Data.SqlClient.SqlConnection();
            try
            {
                SourceConn.ConnectionString = SourceConnectionstr;
                TargetConn.ConnectionString = TargetConnectionstr;
                System.Data.SqlClient.SqlCommand cmd = SourceConn.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = SourceSQLstr;

                SourceConn.Open();
                TargetConn.Open();

                using (System.Data.SqlClient.SqlDataReader rdr = cmd.ExecuteReader())
                {
                    if (!rdr.HasRows)
                    {
                        return false;
                    }
                    using (System.Data.SqlClient.SqlBulkCopy bc = new

                            System.Data.SqlClient.SqlBulkCopy(TargetConn))
                    {
                        //逾時之前操作所允許完成的秒數
                        bc.BulkCopyTimeout = 360;
                       

                        //目標伺服器上的表名
                        bc.DestinationTableName = TargetTableName;
                        bc.WriteToServer(rdr);                       
                    }
                }
            }
            catch
            {
                throw;
            }
            finally
            {

                //關閉串連
                SourceConn.Close();
                TargetConn.Close();
            }

            return true;

}

 

           如果想以最少資源擷取最佳效能,就考濾使用IDbDateReader參數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.