Solved

Compare 2 DataTables

Posted on 2002-07-20
3
7,521 Views
Last Modified: 2009-09-11
I will have 2 populated data tables:  

dtMyTable1
dtMyOtherTable

Here is the interface of the method that I would like to see a working example of:
Public Sub(byval Dt1 as datatable, byval Dt2 as datatable, byref DtInBoth as datatable, byRef DtOnlyInFirst as DataTable)

(I know, this is probably really simple to accomplish, but I am new to .NET...)

Definition of the fields being passed in:
Dt1 --> First data table to compare
Dt2 --> Second data table to compare
DtInBoth -->  Return datatable that contains only the rows found in both datatables
DtOnlyInFirst -->  Return datatable that contains the records that were only found in the first datatable

I would prefer an example in VB.NET, but will accept C# as well.
0
Comment
Question by:tnewc59
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 23

Expert Comment

by:naveenkohli
ID: 7167733
Are the two tables have same schema?
Do both tables have any relations to each other?
Are you loking for comparing each and every field in both tables?

What is your comparison criteria?

If you could provide some details .. may be there is an easy way?
0
 
LVL 1

Author Comment

by:tnewc59
ID: 7168224
Sure.  Here are some more details:

The tables 'should' have the same schema.  At a minumum, they will have the same primary key field.  

The tables will both be dynamically created, but the following is an example of what will typically occur:

Dt1 would have the following rows:
PK_FIELD1     CODE_NAME      CODE_COMMENT
1001          'TEST'         'ANOTHER TEST'
1002          'HELLO'        'DESCRIPTION OF HELLO'


Dt2 would have the following:
PK_FIELD1     CODE_NAME     CODE_COMMENT
1001          'TEST'       'ANOTHER TEST'

The return tables should be filled as follows:
DtInBoth:
PK_FIELD1      CODE_NAME        CODE_COMMENT
1001           'TEST'           'ANOTHER TEST'

DtOnlyInFirst:
PK_FIELD1      CODE_NAME        CODE_COMMENT
1002           'HELLO'          'DESCRIPTION OF HELLO'



In reality, I will need to compare the schemas as well, but I was hoping to see an example of the above comparisions to get me started down the right path.  The code that I really need is a bit more complex, but I think I can figure it out once I see a 'seed' idea.  If the solution I accept has the ability to gracefully handle small schema differences (other than the pk field being different as that will not occur) I will up the points.  
0
 
LVL 1

Accepted Solution

by:
wford earned 100 total points
ID: 7168862
you can compare rows and columns of the datatables..

I dont think there is a standard compare function but a simple row sweep would go something like this(but only if the schema was the same

Private Sub X(ByVal Dt1 As DataTable, ByVal Dt2 As DataTable, ByRef DtInBoth As DataTable, ByRef DtOnlyInFirst As DataTable)
    Dim RowX As DataRow
    DtInBoth = New DataTable()
    DtOnlyInFirst = New DataTable()
    Dim foundrow As DataRow
    For Each rowx In dt1.rows
        foundrow = Dt2.Rows.Find(RowX.Item("PK_FIELD1"))
        If foundrow Is Nothing Then
            DtOnlyInFirst.Rows.Add(RowX)
        Else
            DtInBoth.Rows.Add(foundrow)
        End If
    Next
End Sub

0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question