Solved

Compare 2 DataTables

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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

751 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