比较2个数据表以找出差异 [英] Compare 2 datatables to find difference

查看:68
本文介绍了比较2个数据表以找出差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已被多次询问并且我已经阅读了它们,但我的问题有点不同。



我有2张桌子。 (不是sql表,这些是c#DataTables)。



tab1(原始表):

col A

col B

col C



和tab2(原件的副本。此表的数据可能会被用户修改):< br $>
col A

col B

col C



我想做什么比较tab2到tab1并找到差异。

但是,我想要的更详细一点:



1)查找所有行在tab2中但不在tab1中,即,用户生成的新行

并使用这些行在数据库上执行插入。



2)查找tab1中但不在tab2中的所有行,即用户已删除的行,并使用它们在数据库上执行删除/软删除。



3)查找除上述2个步骤中选择的行之外的所有行,并在DB上执行更新。在这里,这些行是否确实已更新无关紧要。但是如果我们只找到更新的行会更好...



任何想法和帮助都表示赞赏。





更新1:

A)表是C#DataTables,而不是sql / mysql数据库表。我们在DataTables中找到差异,然后在DB表中插入/更新/删除这些行。所以我想比较两个c#DataTables。



B)已经在复制表上调用了AcceptChanges。这是因为这些表绑定到DataGridView。如果存在此类相关列,则列A或B的值的更改会导致列C中的相关更改。例如,如果列C的值=列A +列B ...

并且这些更改需要实时反映到DataGridView。所以比较是在一个原始的DataTable和另一个DataTable的修改副本之间。

I know this question has been asked multiple times and i have read them, but my question goes a bit differently.

I have 2 tables. (not sql tables, these are c# DataTables).

tab1 (the original table):
col A
col B
col C

and tab2 (copy of the original. this table's data may be modified by the user):
col A
col B
col C

what i want to do is compare tab2 to tab1 and find the diference.
but, what i want is a little more detailed:

1) Find all rows that are in tab2 but not in tab1 , ie., new rows generated by the user
and use these to preform insert on the DB.

2) Find all rows that are in tab1 but not in tab2, ie., rows that have been deleted by the user and use them to perform delete/soft delete on the DB.

3) Find all the rows excluding those selected in the above 2 steps and perform an update on the DB. here, it doesnt matter if these rows have indeed been updated. But it would be better if we could find only the updated rows...

Any ideas and help are appreciated.


Update 1:
A) The tables are C# DataTables, not sql/mysql DB tables. We find the difference in the DataTables and insert/update/delete those rows later from the DB tables. So I want to compare two c# DataTables.

B) AcceptChanges has already been called on the copy Table. This is because these tables are bound to a DataGridView. Changes in value of column A or B causes related changes in column C in case there are such related columns. For example if value of column C = column A + column B...
And these changes need to be reflected realtime to the DataGridView. So the comparision is between one original DataTable and another modified copy of the DataTable.

推荐答案

首先你尝试了什么? [ ^ ]



我不会给你准确的解决方案因为我看不到足够的努力?但是你可以通过JOIN,EXCEPT或EXIST等几个方法来解决你的问题。

一个例子是

First of all what have you tried?[^]

I will not give you exact solution since I don't see enough effort? But you can solve your problems by several things such as JOINs,EXCEPT or EXIST.
An example will be
(   SELECT * FROM table1
    EXCEPT
    SELECT * FROM table2)
UNION ALL
(   SELECT * FROM table2
    EXCEPT
    SELECT * FROM table1)



对于SQL连接: w3School SQL JOIN [ ^ ]



还有一些参考链接:

链接#1 [ ^ ]

Link#2 [ ^ ]

链接#3 [ ^ ]



祝你好运,

OI


For SQL joins: w3School SQL JOIN[^]

And some more reference links:
Link #1[^]
Link #2[^]
Link #3[^]

Good luck,
OI


这篇关于比较2个数据表以找出差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆