Solved

Compare 2 DataTables

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

11 Experts available now in Live!

Get 1:1 Help Now