Question Any simple way for compare two datatables ?

nelson.enlis

Member
Joined
Feb 16, 2009
Messages
8
Programming Experience
Beginner
Hi,

I try compare two dataTables. Here a example:
VB.NET:
       OldDt = Dt      ' The idea is keep the old values before refresh the dataTable
       refresh(Dt)      ' refresh this dataTable with the SQL Table.

        If Equals(OldDt, Dt) Then
            Debug.WriteLine("true")
        Else
            RaiseEvent TableChange()    ' its a trigger for detect changes
            Debug.WriteLine("false")
        End If

But, This doesn't work.

I try to debug this sequence but I'm not shure what is the problem.

When the SQL table changes (Because a value is inserted with SQL query), the refresh method make that 'Dt' refresh. After refresh the 'OldDt' take apparently the same data values that 'Dt' (Is how if 'oldDt' was LINKED to 'Dt')

Any idea ?

best regards
 
Last edited:
I'm not realy sure but it could be that you do not copy the values in the second datatable but you just tell that datatable where the first table gets its data. So the data is not in memory 2 times. only once but both datatables use that data. So if one of the tables changes the data the other will see the changes.

Try this

VB.NET:
Dim oldDt As DataTable = dt.Copy()

That might do the trick
 
Dear Thomas

I tried implement your suggestion, but, the instance of compare (equals) always evaluate 'false'.

So, I think that also the equals method not work.
Dim oldDt As DataTable = dt.Copy() ' here you suggestion
refresh(dt) '

If Equals(OldDt, dt) Then
Debug.WriteLine("true")
Else
RaiseEvent TableChange()
Debug.WriteLine("false")
End If
So, exist another way of compare two datatables ?

best regards
 
You could merge your 2 datatables and then call the getchanges function.

This would give you an empty datatable when your 2 tables are equal
 
if you are trying to find out which rows have changed then you might want something more like this:

VB.NET:
For Each drRow as DataRow in dtTable.Rows
     'Examine the drRow.RowState to see what it is then process from there
     Select Case drRow.RowState
          Case DataRowState.Added
               'Process for an added/inserted row
          Case DataRowState.Deleted
                'Process for a row that has been deleted
          Case DataRowState.Modified
                'Process for a row that has been changed
     End Select
Next
 
if you are trying to find out which rows have changed then you might want something more like this:

VB.NET:
For Each drRow as DataRow in dtTable.Rows
     'Examine the drRow.RowState to see what it is then process from there
     Select Case drRow.RowState
          Case DataRowState.Added
               'Process for an added/inserted row
          Case DataRowState.Deleted
                'Process for a row that has been deleted
          Case DataRowState.Modified
                'Process for a row that has been changed
     End Select
Next

Microsoft made that a whole lot easier for you with their DataTable.GetChanges Method (System.Data) :)
 
Back
Top