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

首頁(yè) > 編程 > C# > 正文

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

2020-01-24 00:25:30
字體:
供稿:網(wǎng)友

本文章以一個(gè)表為例,要轉(zhuǎn)多個(gè)表則可將DataSet關(guān)聯(lián)多個(gè)表,下面給出完整代碼,包括引用以及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";  //實(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í)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 高平市| 靖远县| 卢氏县| 三明市| 浮山县| 印江| 桦甸市| 太仓市| 惠来县| 阿鲁科尔沁旗| 瑞丽市| 错那县| 乌海市| 平凉市| 紫金县| 夏津县| 郧西县| 昔阳县| 开远市| 安平县| 湖口县| 酒泉市| 荃湾区| 任丘市| 青田县| 乐安县| 罗山县| 策勒县| 宜章县| 金寨县| 望江县| 曲沃县| 北海市| 延庆县| 日照市| 尉犁县| 海盐县| 贵州省| 元阳县| 灵宝市| 绥阳县|