2014-03-25 107 views
0

我正在使用c#,asp.net 我有兩個不同的表格來自兩個不同的數據庫。 都有一個共同的領域,我有興趣說custid
我已經分配了兩個數據表,但我想要做的是這個
比較兩個數據表(可能與一個dataview,我從來沒有用過)
看哪個custid從任何表中缺少,
然後我可以從第一個表中插入或刪除它們,因爲我希望第一個表具有與第二個表相同的custid。比較來自不同數據庫的不同表格

 
    Table 1   Table 2 
    1    1 
    2       - want to delete from table 1 
    3    3 
        4   - want to add to table 1 



任何幫助,將不勝感激。

+3

爲什麼見datagridview,你甚至可以自動化它。在Table1上循環檢查每個custid,如果在Table2中沒有找到,則刪除,類似地在Table2上循環,如果未找到,則添加到Table1。如果你仍然有興趣在datagridview中查看它們,你可以參考'http:// code.msdn.microsoft.com/DB-Invader-A-Database-6fd576f8'。這是贏得應用程序。 –

+0

沒有沒有datagridview,一個數據視圖,所以我不必做循環。 – Rachsherry

+0

你會(a)向用戶顯示兩個表,或者(b)按照上述條件簡單地執行添加刪除。如果(b)是這種情況,那麼您可以在custid和filterout行上的兩個數據表上建立一個連接來刪除和添加。如果(a)那麼你還需要在所述行上按要求顯示添加/刪除按鈕,即使你可以加入兩個數據表(全部外部)和顯示按鈕。 –

回答

0

,因爲我不想要顯示的數據只是操縱它

我到底做了什麼

 
     string [] servercustomers = new string [servercustomertable.lenght]; 
     for (i = 0; i < servercustomertable.length; i++) 
      { 
      // run query to see if current record exists in localtable 
      // if it does update it 
      // if it doesnt create it 
      servercustomer[i] = servercustomertable.Rows[i]["Custid"]; 
      } 
     // check through the localtable 
     for (i = 0; i < localcustomertable.length; i++) 
      { bool recordexist = false; 
       // check through the array to see if the custid is there 
       for (j = 0; j < servercustomer.length; j++) 
       { 
        if (servercustomer[j] = localcustomertable.Rows[i]["custId"]) 
         { 
          recordexists = true; 
          break; 
         } 
         if(!recordexists) 
          // delete the record from the local table 
        } 
      } 


感謝您的幫助 瑞秋