2009-10-24 117 views
0

我有兩個數據表。兩個數據表中有幾個相同的列。現在我需要比較兩個數據表中的每個相同列的單元格,並通過合併相同的更改來構建第三個數據表列的單元格和不同的列。請幫助我使用C#代碼。比較和合並兩個數據表的相同列

感謝, Vix指數

回答

2

可以使用DataTable的Merge()方法來完成。

合併方法比較鍵列並將行合併到一個表中。

// Set the identical columns to compare by in first table 
table1.PrimaryKey = new DataColumn[] 
         { idColumnOfTable1, anotherIDColumnOfTable1 }; 
// Set the identical columns to compare by in second table 
table2.PrimaryKey = new DataColumn[] 
         { idColumnOfTable2, anotherIDColumnOfTable2 }; 

// The MissingSchemaAction.Add will add the non-identical columns 
// Non-identical columns existing in from table 2 will be added to table1 
table1.Merge(table2, false, MissingSchemaAction.Add); 
+0

請問您可以幫助我的代碼。如何爲特定數量的列完成? – Vix 2009-10-24 09:05:58

+0

我是指每個特定的列。 – Vix 2009-10-24 09:24:15

+0

當然,更新的答案。希望能幫助到你。 – Elisha 2009-10-24 09:46:39