find differences between two datasets

  • Thread starter Thread starter Sankar Nemani
  • Start date Start date
S

Sankar Nemani

Hi
I have two datasets that have the same schema. They have different data.
Both are in an unchanged state which means when I do a ds1.HasChanges or
ds2.HasChanges I get false.
Now how do I find the differences between the two?
I tried ds1.Merge(ds2) and then tried ds1.GetChanges. But I still get
nothing.
TIA
Sankar Nemani
 
Sankar:

The problem is that neither DataSet has changes, so when you merge them,
they aren't going to have them either. Typically, you'd do a GetChanges and
merge the DataTable with the changes with the dataset. The RowsState is
carried over with the merge so if nothing is marked as
Added/Deleted/Modified then (ie HasChanges = false) then you won't have any
changes afterward.

Assuming Ds2 didn't have changes in it, there's really no difference between
calling GetChanges before or after the merge.

The only way I know of comparing them is to loop through the rows and
columns and and compare them one at a time. You can subclass your datasets
and implement ICOmparable or similar functionality..here's a simplified
shell but you'll need to modify it to check for nulls (also, this is very
simple, you couldn't necessarily want to cast eveything to string, but you
could GetType the column in advance and use that type for the ctype

For i As Integer = 0 To ds1.Tables(0).Rows.Count - 1

For x As Integer = 0 To ds1.Tables(0).Columns.Count - 1

If CType(ds1.Tables(0).Rows(i)(x), String) <>
CType(ds1.Tables(0).Rows(i)(x), String) Then

GotChanges = False

End If

Next

Next



HTH,



Bill
 
Thanks for Bill's great response!

Hi Sankar,

Thank you for posting in the community!

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to compare the difference
between two DataSets. However, using GetChanges method after merging
doesn't work. If there is any misunderstanding, please feel free to let me
know.

Just as Bill said, the Merging method will not modify the RowState property
in tables. If the original rowstate is unchanged, it remains to be
unchanged after merging. For workaround, Bill has provided us with a good
solution, to compare the rows one by one. Here I have another way.

If you have a primary key set in the tables, we can add Ds2 rows to Ds1 one
by one. During adding, if there are two duplicate rows, a
ConstraintException will be thrown. We can just catch that exception and do
nothing, so that only the different rows will be added. Here I have written
a code snippet.

private DataSet GetDifference(DataSet ds1, DataSet ds2)
{
foreach(DataRow dr in ds1.Tables[0].Rows)
{
try
{
ds1.Tables[0].Rows.Add(dr.ItemArray);
}
catch
{
}
}
return ds1.GetChanges(DataRowState.Added);
}

HTH. If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
But then only new rows are added and changes to columns other than the
primary key are not shown in the ds2.
If there is no other solution, I think I will have to do what Bill says
(going through each row and column to find changes).
Thank you
Sankar
 
Hi Sankar,

The solution I have provided depends on the data and primary key in the
current DataSets. There has to be primary keys set in both tables, and
DataSet identifies data through primary keys. The duplicated rows have the
same primary key, while the different rows has different keys. So my
solution only applies to DataSets which meets these conditions. If not so,
I think you have to try Bill's suggestion.

If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Back
Top