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í)例名稱為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í)有所幫助,也希望大家多多支持我們。
欄 目:C#教程
下一篇:C#獲取Visio模型信息的簡(jiǎn)單方法示例
本文標(biāo)題:C#實(shí)現(xiàn)復(fù)制數(shù)據(jù)庫(kù) C#將A數(shù)據(jù)庫(kù)數(shù)據(jù)轉(zhuǎn)到B數(shù)據(jù)庫(kù)
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5429.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新聞效果的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實(shí)現(xiàn)多線程下載文件的方法
- 01-10C#實(shí)現(xiàn)Winform中打開(kāi)網(wǎng)頁(yè)頁(yè)面的方法
- 01-10C#實(shí)現(xiàn)遠(yuǎn)程關(guān)閉計(jì)算機(jī)或重啟計(jì)算機(jī)的方法
- 01-10C#自定義簽名章實(shí)現(xiàn)方法
- 01-10C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法
- 01-10winform實(shí)現(xiàn)創(chuàng)建最前端窗體的方法


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10delphi制作wav文件的方法
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-11Mac OSX 打開(kāi)原生自帶讀寫NTFS功能(圖文