2011-12-30 99 views
0

我正在C#中執行以下語句。 有兩個表Orders,Data。 訂單是一個MySQL數據庫。 數據是MSSQL數據庫。比較兩個表,一個是mysql數據庫,一個是MSSQL數據庫

insert into orders (orders_id, customers_id, customers_cid, customers_vat_id, customers_name, customers_email_address) 
select 
    o.* 
from 
    Test.dbo.orders o 
where 
    not exists (
     select 1 
     from 
      CobraDemoData.dbo.Data a 
     where 
      a.email0 = o.customers_email_address 
    ) 

兩個數據庫

MySqlConnection sqlConn = new MySqlConnection("server=localhost;User Id=root;Password = 123456;Persist Security Info=True;database=xtcommerce";) 

SqlConnection con1 = new SqlConnection(@"Data Source=SUBASH-LAPTOP\COBRA;Initial Catalog=CobraDemoData;Integrated Security=True"); 

我已經創建的連接字符串可以請你告訴我下一步怎麼做?

任何幫助將不勝感激.. 謝謝, Subash ....

+0

除非MSSQL或MySQL包含鏈接在他們的數據庫表中的行,你將無法使用需要的方法來更新。您必須從一箇中獲取所有記錄,然後通過更新另一個循環來循環,使用LINQ管理不同數據庫之間的連接,或者從一個導出,將其導入到temp中,然後再從temp中插入。這不是一個答案,因爲我不知道如何暗示,我只知道這些是一些比較常見的選項。原因:您無法通過一個連接連接到兩個數據庫。 – xQbert 2011-12-30 13:58:58

+0

如果這是*一次性*類型的項目,您還可以將MySQL數據導入到MSSQL服務器中並導入到表中並從那裏開始工作。通過這種方式,您可以加入數據等。如果這是長期項目的要求,那麼請忽略此建議。 – user1231231412 2011-12-30 14:07:22

回答

0

打開兩個讀者查詢哪兩個表有相同鍵與類似的查詢命令,並通過行做對比行,類似的東西,(假設有兩個連接 - MSSQL和MySQL:

var sc1 = mssql.CreateCommand(); 
var sc2 = mysql.CreateCommand(); 
sc1.CommandText = sc2.CommandText = "select id, name, email, phone from yourtable ORDER BY Id"; 
sc1.CommandTimeout = sc2.CommandTimeout = 0; 

      using (var r1 = sc1.ExecuteReader()) 
      { 
       using (var r2 = sc2.ExecuteReader()) 
       { 
        while (true) 
        { 
         bool read1 = r1.Read(); 

         if (read1^r2.Read()) 
          throw new Exception("Doesn't match!"); 

         if(!read1) {Console.WriteLine("MATCH!!!"); break;} 

         for (int i = 0; i < r1.FieldCount; i++) 
         { 
          if (r1.IsDBNull(i)^r2.IsDBNull(i)) 
           throw new Exception("Doesn't match!"); 

          if (string.Compare(r1[i].ToString(), r2[i].ToString(), StringComparison.InvariantCultureIgnoreCase) != 0) 
           throw new Exception("Doesn't match!"); 

         } 
        } 
       } 
      } 

基於這個例子中,您可以再實現你的更新邏輯

有必須是一些獨特的領域,其中確定兩個表

相關問題