C#實(shí)現(xiàn)復(fù)制數(shù)據(jù)庫(kù) C#將A數(shù)據(jù)庫(kù)數(shù)據(jù)轉(zhuǎn)到B數(shù)據(jù)庫(kù)
本文章以一個(gè)表為例,要轉(zhuǎn)多個(gè)表則可將DataSet關(guān)聯(lián)多個(gè)表,下面給出完整代碼,包括引用以及main函數(shù)與復(fù)制函數(shù)。
要說(shuō)明的是,必須先用Sql語(yǔ)句復(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"; //實(shí)例名稱(chēng)為CANFLY builder1.InitialCatalog = "desdata"; //目標(biāo)數(shù)據(jù)庫(kù) builder1.IntegratedSecurity = true; //使用Windows身份驗(yàn)證 SqlConnectionStringBuilder builder2 = new SqlConnectionStringBuilder(); builder2.DataSource = ".\\CANFLY"; builder2.InitialCatalog = "bddata"; //源數(shù)據(jù)庫(kù) builder2.IntegratedSecurity = true; //調(diào)用復(fù)制數(shù)據(jù)庫(kù)函數(shù) InsertTable(builder1.ConnectionString, builder2.ConnectionString, table); } //參數(shù)為兩個(gè)數(shù)據(jù)庫(kù)的連接字符串 private static void InsertTable(string conString1, string conString2, string tabStr) { //連接數(shù)據(jù)庫(kù) 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ù)庫(kù) SqlCommandBuilder cb = new SqlCommandBuilder(adapter1); adapter1.Update(dataSet1, tabStr); cb.RefreshSchema(); Console.WriteLine("表" + tabStr + "復(fù)制成功!"); conn1.Close(); conn2.Close(); } } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#客戶(hù)端HttpClient請(qǐng)求認(rèn)證及數(shù)據(jù)傳輸
本文詳細(xì)講解了C#客戶(hù)端HttpClient請(qǐng)求認(rèn)證及數(shù)據(jù)傳輸,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01C#?TaskScheduler任務(wù)調(diào)度器的實(shí)現(xiàn)
本文主要介紹了C#?TaskScheduler任務(wù)調(diào)度器的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧<BR>2023-05-05C#讀取xml節(jié)點(diǎn)數(shù)據(jù)方法小結(jié)
這篇文章主要介紹了C#讀取xml節(jié)點(diǎn)數(shù)據(jù)的方法,實(shí)例總結(jié)了C#針對(duì)XML文件節(jié)點(diǎn)操作的相關(guān)技巧,需要的朋友可以參考下2015-06-06C#字符串左不足位數(shù)時(shí)補(bǔ)充0的幾種方式
想讓一個(gè)整數(shù)或字符串轉(zhuǎn)換為字符串后,如果其長(zhǎng)度不足5位,則在左邊補(bǔ)充0直到達(dá)到5位,本文給大家介紹了C#字符串左不足位數(shù)時(shí)補(bǔ)充0的幾種方式,感興趣的朋友可以參考下2024-04-04C#結(jié)合SMTP實(shí)現(xiàn)郵件報(bào)警通知的實(shí)現(xiàn)示例
這篇文章主要介紹了C#結(jié)合SMTP實(shí)現(xiàn)郵件報(bào)警通知的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07C# SaveFileDialog與OpenFileDialog用法案例詳解
這篇文章主要介紹了C# SaveFileDialog與OpenFileDialog用法案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08