Comparing 2 datasets (or datatables)

G

Guest

I want to compare two datasets (of 2 datatables). I just need to know whether
the 2 datasets are same or different. I do not want to know how it differs.
(I would be nice to have though).. Is there any easy way to accompilsh this.

Thanks
 
M

Mona

Hi Raja,

To compare two datasets and also to find out the difference create a third and a fourth dataset.
Merge this third dataset with the first dataset. Do an Acceptchanges(). Then, merge it with the
second dataset. Finally, do a GetChanges() on the third dataset and store it in the fourth dataset.

Following is a code snippet demonstrating the method to do so:

ds3.Merge(ds1)
ds3.AcceptChanges()
ds3.Merge(ds2)
ds4 = ds3.GetChanges()

HTH

Mona[Grapecity]



that
 
G

Guest

Hi Mona,
Thanks for your reply.. But the ds4 is alway coming as null..

Here is what I am doing,

SqlDataAdapter myAdapter1 = new SqlDataAdapter(sql1,myConnection);
DataSet ds1 = new DataSet();
myAdapter1.Fill(ds1);

(Sql1 query returns,
employeeId
--------------
NULL
2
3
)


DataSet ds2 = new DataSet();
SqlDataAdapter myAdapter2 = new SqlDataAdapter(sql2,myConnection);
myAdapter2.Fill(ds2);

(Sql2 query returns,
employeeId
--------------
1
2
3
)

DataSet ds3 = new DataSet();
ds3.Merge(ds1);
ds3.AcceptChanges();
ds3.Merge(ds2);

ds4 = ds3.GetChanges(DataRowState.Modified);

if (ds4 != null)
{
ds4.WriteXml("DiffGramFile.xml");
}
else
{
Debug.WriteLine("Dataset 4 is null");
}

Is there anything I am doing worng.

Thanks in advance.

Mona said:
Hi Raja,

To compare two datasets and also to find out the difference create a third and a fourth dataset.
Merge this third dataset with the first dataset. Do an Acceptchanges(). Then, merge it with the
second dataset. Finally, do a GetChanges() on the third dataset and store it in the fourth dataset.

Following is a code snippet demonstrating the method to do so:

ds3.Merge(ds1)
ds3.AcceptChanges()
ds3.Merge(ds2)
ds4 = ds3.GetChanges()

HTH

Mona[Grapecity]



that
Raja said:
I want to compare two datasets (of 2 datatables). I just need to know whether
the 2 datasets are same or different. I do not want to know how it differs.
(I would be nice to have though).. Is there any easy way to accompilsh this.

Thanks
 
S

Sambathraj

Hi,
Simplest way to check whther two dataset are same is to merge one with
another and Check for changes using HasChanges()
I will give you a sample soon
Regards,
Sambath
 
S

Sahil Malik [MVP]

Raja,

For an operation such as yours, I'd hate to have to overhead of converting
to either a string (xml), or Merge or GetChanges - because all of these are
expensive operations. GetXml isn't so bad .. but still.

So before I answer your question, let me ask you a question - The two
datasets that you have of two datatables each - do they necessarily have the
same structure? I mean, are you only comparing data, or you also might need
to compare for the existence or nonexistence of a datatable.

If performance isn't important to you then GetXML and compare the strings is
the best option.

- Sahil Malik [MVP]
http://codebetter.com/blogs/sahil.malik/
 
G

Guest

Sahil,
For me both the dataset will always have only one table and the table
strucure will be always same.. only differene will be the date.. that
includes primary key data..
Below is the example of two sets of data that are there in dataset 1 and 2
resply..

employeeId
--------------
1
2
3

employeeId
--------------
NULL
2
3

I should get the result as, two datasets are different and they are differed
by the employeeId column and the value is NULL vs 1.

Thanks
 
S

Sahil Malik [MVP]

Raja,

If performance is important to you, I'd implement the simple checks first.

You know the only two places differences can be are -

a) Number of rows.
b) The actual data.

So just write a function that compares the two datatables for you.

In the function the first check should be rows.count. If it's different,
return true
Second check - start iterating through rows and columns - the first
difference you find, return a true.

This in my mind will be the most efficient/fastest way of comparing two
structurally identical datatables. You might even be able to think up of
further optimizations.

- Sahil Malik [MVP]
http://codebetter.com/blogs/sahil.malik/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top