溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C#實現(xiàn)復(fù)制數(shù)據(jù)庫 C#將A數(shù)據(jù)庫數(shù)據(jù)轉(zhuǎn)到B數(shù)據(jù)庫

發(fā)布時間:2020-08-20 10:43:30 來源:腳本之家 閱讀:178 作者:至濁至愚 欄目:編程語言

本文章以一個表為例,要轉(zhuǎn)多個表則可將DataSet關(guān)聯(lián)多個表,下面給出完整代碼,包括引用以及main函數(shù)與復(fù)制函數(shù)。
要說明的是,必須先用Sql語句復(fù)制表結(jié)構(gòu),才能順利的使用以下代碼復(fù)制數(shù)據(jù)。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Data; 
using System.Data.SqlClient; 
using System.Data.Common; 
 
namespace CopyData 
{ 
 class Program 
 { 
  static void Main(string[] args) 
  { 
   //要復(fù)制的表名 
   string table = "V_Position"; 
    
   //構(gòu)造連接字符串 
   SqlConnectionStringBuilder builder1 = new SqlConnectionStringBuilder(); 
   builder1.DataSource = ".\\CANFLY";  //實例名稱為CANFLY 
   builder1.InitialCatalog = "desdata"; //目標(biāo)數(shù)據(jù)庫 
   builder1.IntegratedSecurity = true;  //使用Windows身份驗證 
 
   SqlConnectionStringBuilder builder2 = new SqlConnectionStringBuilder(); 
   builder2.DataSource = ".\\CANFLY"; 
   builder2.InitialCatalog = "bddata";  //源數(shù)據(jù)庫 
   builder2.IntegratedSecurity = true; 
 
   //調(diào)用復(fù)制數(shù)據(jù)庫函數(shù) 
   InsertTable(builder1.ConnectionString, builder2.ConnectionString, table); 
  } 
   
  //參數(shù)為兩個數(shù)據(jù)庫的連接字符串 
  private static void InsertTable(string conString1, string conString2, string tabStr) 
  { 
   //連接數(shù)據(jù)庫 
   SqlConnection conn1 = new SqlConnection(); 
   conn1.ConnectionString = conString1; 
   conn1.Open(); 
 
   SqlConnection conn2 = new SqlConnection(); 
   conn2.ConnectionString = conString2; 
   conn2.Open(); 
 
   //填充DataSet1 
   SqlDataAdapter adapter1 = new SqlDataAdapter("select * from " + tabStr, conn1); 
   DataSet dataSet1 = new DataSet(); 
 
   if (dataSet1 != null) 
   { 
    adapter1.Fill(dataSet1, tabStr); 
   } 
 
   SqlDataAdapter adapter2 = new SqlDataAdapter("select * from " + tabStr, conn2); 
   DataSet dataSet2 = new DataSet(); 
 
   SqlCommand cmd2 = new SqlCommand("select count(*) from " + tabStr, conn2); 
   Object res2 = cmd2.ExecuteScalar(); 
 
   if (res2 != null) 
   { 
    int nCount = Convert.ToInt32(res2.ToString()); 
    if (nCount == 0) 
    { 
     conn1.Close(); 
     conn2.Close(); 
     return; 
    } 
   } 
 
   //填充DataSet2 
   if (dataSet2 != null) 
   { 
    adapter2.Fill(dataSet2, tabStr); 
   } 
 
   //復(fù)制數(shù)據(jù) 
   for (int j = 0; j < dataSet2.Tables[0].Rows.Count; j++) 
   { 
    dataSet1.Tables[0].LoadDataRow(dataSet2.Tables[0].Rows[j].ItemArray, false); 
   } 
 
   //將DataSet變換顯示在與其關(guān)聯(lián)的目標(biāo)數(shù)據(jù)庫 
   SqlCommandBuilder cb = new SqlCommandBuilder(adapter1); 
   adapter1.Update(dataSet1, tabStr); 
   cb.RefreshSchema(); 
 
   Console.WriteLine("表" + tabStr + "復(fù)制成功!"); 
 
   conn1.Close(); 
   conn2.Close(); 
 
  } 
 } 
} 

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI