Solved

Compare 2 DataTables

Posted on 2002-07-20
3
7,516 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
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
Send secure, cloud-based, encrypted alerts and maintain HIPAA compliant messaging. Integrates priority & secure messaging into one application. Ensures IT, emergency respondents and healthcare professionals that their critical messages are never mis…

911 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now