比较数据表以找出差异 [英] compare datatables to find differences

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

问题描述

我知道这个问题或类似的问题已多次被问过...

我已经用这个代码来比较两个数据表。我需要的是更复杂的东西。



我所拥有的是DataTable名称originalTable,其中包含来自数据库的一些数据。

可以说数据如下:



Col-1 Col-2 Col-3

1 ax

2 by

3 cs

4 dq

5 ew



现在,我使用



 DataTable copyTable = originalTable.Copy(); 





copyTable用于交叉检查,不会被修改。原始表将绑定到DataGridView并可能被修改。



修改可以是3种类型:



插入一个全新的行,修改现有行或删除现有行。



例如,假设第3行被删除而我添加了一行,即第6行在DataGridView中第3行的相同索引/位置。



Col-1 Col-2 Col-3

1斧

2 by

6 fr

4 dq

5 ew



我需要做的是,我需要点击保存,然后发生以下情况:





 originalTable.AcceptChanges(); 
DataTable updatedData = CompareTables(originalTable,copyTable);







这里,我需要updatedData正确识别



Row3已被删除,Row6已被添加到row3的位置,因此,这不是更新操作,而是删除和插入操作......



或者说,我把row7添加到最后。

在我保存之前,我可以看到datagridview中的row7 。我一次又一次地修改,修改和修改row7。在这种情况下,CompareTables必须认识到它是一个INSERT操作,因为尚未插入row7。因此,必须插入row7,并在单击保存之前对其进行最新修改。



必须使用数字1,2,3 ,4 ...我用于行引用是唯一的,永远不会重复,即使该行已被标记为已删除。

新行必须始终具有MAX(数字)+ 1,即,无论我在哪里插入新行都不会改变它的数量将大于最新行数并以连续方式进展的事实。



我已经拥有此功能:



  public   static  DataTable CompareRows(DataTable sourceTable,DataTable checkTable)
{
DataTable resultTable = sourceTable.Clone();
resultTable.Clear();
if (checkTable.Rows.Count == 0
{
resultTable = sourceTable.Copy();
}
else
{
for (< span class =code-keyword> int i = 0 ; i < checkTable .Rows.Count; i ++)
{
var sourceArray = sourceTable.Rows [i] .ItemArray;
var checkArray = checkTable.Rows [i] .ItemArray;
if (!sourceArray.SequenceEqual(checkArray))
{
DataRow rtRow = resultTable.NewRow();
rtRow.ItemArray = sourceTable.Rows [i] .ItemArray;
resultTable.Rows.Add(rtRow);
}
}
}
return resultTable;
}





但这并不适用于所有情况。我需要一个非常通用的比较函数,它可以正确识别在originalTable上执行的每个INSERT,MODIFY和DELETE,并返回带有相应标记的那些行,也许在具有3个表的DataSet中,即

insertTable,modifyTable和deleteTable,每个包含适当的行...

解决方案

您好b $ b检查:

http://rakeshbajania.wordpress.com/2011/01/13/compare-data-tables/ [ ^ ]



http://forgetcode.com/CSharp/1508-Comparing-two-datatables-and-returning-a-datatable-by-matching-one-or-more-columns-using- LINQ [ ^ ]

I know this question or a similar has been asked many times over...
I already did this code to compare two datatables. what i need is something a bit more complex.

What I have is a DataTable names originalTable, populated with some data from a DB.
Lets say the data is as follows:

Col-1 Col-2 Col-3
1 a x
2 b y
3 c s
4 d q
5 e w

Now, I make an exact copy of this table using

DataTable copyTable = originalTable.Copy();



The copyTable is used for cross checking purposes and will not be modified. The original table will be bound to a DataGridView and may get modified.

The modification can be 3 types:

Insertion of a whole new row, Modification of existing row or Deletion of existing row.

for example, suppose row 3 gets deleted and I add a new row, namely Row 6 in the same index/position of row 3 in the DataGridView.

Col-1 Col-2 Col-3
1 a x
2 b y
6 f r
4 d q
5 e w

What i need to do is that, I need to do something like click 'save' and the below happens:


originalTable.AcceptChanges();
DataTable updatedData = CompareTables(originalTable, copyTable);




Here, i need the updatedData to correctly identify that

Row3 has been deleted, Row6 has been added in place of row3, thus, this is not an update operation, rather a delete and an insert operation...

Or, say I add row7 to the end.
before i save, i can see the row7 in the datagridview. i modify and modify and modify row7 again and again. In this case, CompareTables must recognizethat it is an INSERT operation as row7 has not been inserted. So, row7 has to be inserted with the latest modification that was done to it before 'save' was clicked.

It is necessary that the number 1, 2, 3, 4... which i used for row reference be unique and never duplicated, even though the row has been marked as deleted.
new row must always have the MAX(number) + 1, ie, wherever i insert the new row doesnt change the fact that its number will be greater than the latest row number and progress in a consecutive fashion.

I already have this function:

public static DataTable CompareRows(DataTable sourceTable, DataTable checkTable)
		{
			DataTable resultTable = sourceTable.Clone();
			resultTable.Clear();
			if (checkTable.Rows.Count == 0)
			{
				resultTable = sourceTable.Copy();
			}
			else
			{
				for (int i = 0; i < checkTable.Rows.Count; i++)
				{
					var sourceArray = sourceTable.Rows[i].ItemArray;
					var checkArray = checkTable.Rows[i].ItemArray;
					if (!sourceArray.SequenceEqual(checkArray))
					{
						DataRow rtRow = resultTable.NewRow();
						rtRow.ItemArray = sourceTable.Rows[i].ItemArray;
						resultTable.Rows.Add(rtRow);
					}
				}
			}
			return resultTable;
		}



But this does not do the job in every situation. I need a very general compare function that can correctly identify every INSERT, MODIFY and DELETE performed on the originalTable and return those rows with the appropriate markers, maybe in a DataSet having 3 tables, namely
insertTable, modifyTable and deleteTable, each containing the appropriate rows...

解决方案

Hi Check this:
http://rakeshbajania.wordpress.com/2011/01/13/compare-data-tables/[^]

http://forgetcode.com/CSharp/1508-Comparing-two-datatables-and-returning-a-datatable-by-matching-one-or-more-columns-using-LINQ[^]


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

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