2009-10-24 81 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