PC Review


Reply
Thread Tools Rate Thread

Comparing changes between two DataTables

 
 
Mika M
Guest
Posts: n/a
 
      18th Jan 2005
Hi!

My application uses two DataTables, one retrieved from XML-file
containing "Previous" situation, if application was closed and opened,
and "Current"-table as a copy of "Previous"-table when application is
just started. These both are similar DataTables and containing "ID" as
Unique-field

Application retrieves ID's from reader after certain delays into the
"Current"-table, and it's working fine. When ID's are retrieved from
reader, after that application need to check which ID's are removed or
added comparing between new "Current"- and "Previous"-table, because
it's only possible to get present ID's from reader. After removed or
added ID's are checked, then application adds these events into Event
Log. Finally new "Current"-DataTable is copied as new
"Previous"-DataTable like dtPrevious = dtCurrent.Copy(), and after that
new content of "Previous"-DataTable is saved into XML-file.

My question is, is there better way to compare changes between these
DataTables? Better like the following code below I'm using now? I'm
quite sure yes there must be (Please don't laught)! Any succestions
how to do this better way. Anyway this code works!


'// Create "New Events" DataTable
Dim dtNewEvents As DataTable = New DataTable("Event")
dtNewEvents.Columns.Add("Function", GetType(Integer))
dtNewEvents.Columns.Add("ID", GetType(String))

Dim drNewEvent As DataRow
Dim strID As String

'// *** Check for ID's added after previous situation ***
If (dtPrevious Is Nothing) Then
'// "Previous" table does not exist:
'// -> Mark all ID's directly as Added into Event Log
For Each dr As DataRow In dtCurrent.Rows
drNewEvent = dtNewEvents.NewRow
drNewEvent("Function ID") = 2
drNewEvent("ID") = dr("ID")
dtNewEvents.Rows.Add(drNewEvent)
Next
Else
'// Previous table exists -> Compare DataRows between tables

'// *** Check for ID's added after previous situation ***
For Each drCurrent As DataRow In dtCurrent.Rows
strID = drCurrent("ID").ToString

For Each drPrevious As DataRow In dtPrevious.Rows
If (drPrevious("ID").Equals(drCurrent("ID"))) Then
strID = ""
Exit For
End If
Next

'// ID was not found in Previous table
'// -> Mark as Added into Event Log
If (strID.Length > 0) Then
drNewEvent = dtNewEvents.NewRow
drNewEvent("Function ID") = 2
drNewEvent("ID") = strID
dtNewEvents.Rows.Add(drNewEvent)
End If
Next

'// *** Check for ID's removed after previous situation ***
For Each drPrevious As DataRow In dtPrevious.Rows
strID = drPrevious("ID").ToString

For Each drCurrent As DataRow In dtCurrent.Rows
If (drCurrent("ID").Equals(drPrevious("ID"))) Then
strID = ""
Exit For
End If
Next

'// Does not exist in Current table
'// -> Mark as Removed into Event Log
If (strID.Length > 0) Then
drNewEvent = dtNewEvents.NewRow
drNewEvent("Function ID") = 3
drNewEvent("ID") = strID
dtNewEvents.Rows.Add(drNewEvent)
End If
Next
End If

'// *** Save into Event Log if new events was found ***
If (dtNewEvents.Rows.Count > 0) Then
Dim el As EventsLog = New EventsLog()
el.NewEvents = dtNewEvents
el.SaveNewEvents()
End If

....code continues...


--
Thanks in advance!

Mika
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparing Two DataTables c_shah Microsoft VB .NET 2 24th May 2006 09:16 AM
Comparing 2 datatables... Frank Microsoft VB .NET 5 2nd Feb 2006 07:04 PM
Re: Comparing two DataTables Adrian Moore Microsoft ADO .NET 0 15th May 2005 04:22 PM
Re: Comparing two DataTables Miha Markic [MVP C#] Microsoft ADO .NET 0 15th May 2005 01:38 PM
Re: Comparing two datatables Sahil Malik Microsoft ADO .NET 0 7th Mar 2005 07:46 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:09 AM.