我有 2 个数据表,我只想知道它们是否相同。 “相同”是指它们的行数是否完全相同,每列中的数据是否完全相同。我很想编写(查找)一个接受两个表并返回 bool 值的方法。
如何以这种方式比较 2 个数据表?两者具有相同的架构。
最佳答案
public static bool AreTablesTheSame( DataTable tbl1, DataTable tbl2)
{
if (tbl1.Rows.Count != tbl2.Rows.Count || tbl1.Columns.Count != tbl2.Columns.Count)
return false;
for ( int i = 0; i < tbl1.Rows.Count; i++)
{
for ( int c = 0; c < tbl1.Columns.Count; c++)
{
if (!Equals(tbl1.Rows[i][c] ,tbl2.Rows[i][c]))
return false;
}
}
return true;
}
关于c# - 如何比较2个数据表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7517968/