开发者

Fastest way to determine two datatables contain same data?

I want to verify that 2 datatables contains same data (for unit testing), but unfortunately, Assert.AreEqual does not work, it seems that each datatable contains unique metadata that makes the开发者_JAVA百科ir references not equal.

How can I do this?


If you want to know if they are different:

I would sort the DataTables by their key, then iterate over both at the same time. If the keys are different for the same position, then you know the tables are different. If the keys are the same, compare the other columns of the tables for this row.

If you want to get the differences between them:

You may also want to have a look at the Merge method of the DataTable.


Given that you are talking about SQL server and you have 2 databases on the same server you can run a cross-database EXCEPT per table to get a difference count like so :

SELECT COUNT(*) 
FROM (
    SELECT * FROM database1.dbo.table1
    EXCEPT
    SELECT * FROM database2.dbo.table1
) AS diff

I believe that is the fastest way but then again I made a lot of presumptions about your setup :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜