locked
Compare two datatables with primary keys RRS feed

  • Question

  • I want to compare 2 datatables using primary keys in vb.net app.  here is the code snippet:

     

     

     

    Dim dt1 as dataTable

    Dim dt2 as dataTable

    Dim matchedDT as dataTable

    Dim unMatchedDT as dataTable

    matchedDT =  dt1.clone()

    unMatchedDT = dt1.clone()

     

     

    dt2.PrimaryKey = New DataColumn() {tableDT.Columns(0)), tableDT.Columns(1), tableDT.Columns(2), tableDT.Columns(3)}

     

     

     

            Dim MatchRow As DataRow

     

            For Each dr In dt1.Rows

                MatchRow = dt2.Rows.Find(dt2.PrimaryKey)

     

     

                If MatchRow Is Nothing Then 'If the record is not in Table 2  

                    unMatchedDT.ImportRow(dr)

     

                Else

     

                    matchedDT.ImportRow(MatchRow)

     

                End If

            Next

     

    But it still added  matched Row to unmatched dataTable, it seems it did not check the primary keys. Did I miss anything? Thank you!

    Wednesday, May 12, 2010 7:20 PM

Answers

  • Hello Lydia,

    Welcome to ADO.NET Managed Providers forum!

    Based on your post, I think you want to put the data rows in dt1 whose primary key is among dt2 into matchedDT, and the unmatched data rows into the unmatchedDT, right?  If so, this line of the codes may cause some problems: MatchRow = dt2.Rows.Find(dt2.PrimaryKey).  

    DataTable.Rows.Find() method checks whether the data table contains the data row which has the primary key equals to the passed in key values.   Here dt2.PrimaryKey is a DataColumn objects array instead of an array of primary key values.   So this line of codes may be modified as following:

    MatchRow = dt2.Rows.Find(New Object() { dr(0), dr(1), dr(2), dr(3) })

    Here I assume dt1 has the similar primary key structure with the dt2.  

    If you have any questions, please feel free to let me know.

    Have a nice day!


    Lingzhi Sun
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact
    msdnmg@microsoft.com. 


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, May 13, 2010 3:34 PM

All replies

  • Hello Lydia,

    Welcome to ADO.NET Managed Providers forum!

    Based on your post, I think you want to put the data rows in dt1 whose primary key is among dt2 into matchedDT, and the unmatched data rows into the unmatchedDT, right?  If so, this line of the codes may cause some problems: MatchRow = dt2.Rows.Find(dt2.PrimaryKey).  

    DataTable.Rows.Find() method checks whether the data table contains the data row which has the primary key equals to the passed in key values.   Here dt2.PrimaryKey is a DataColumn objects array instead of an array of primary key values.   So this line of codes may be modified as following:

    MatchRow = dt2.Rows.Find(New Object() { dr(0), dr(1), dr(2), dr(3) })

    Here I assume dt1 has the similar primary key structure with the dt2.  

    If you have any questions, please feel free to let me know.

    Have a nice day!


    Lingzhi Sun
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact
    msdnmg@microsoft.com. 


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, May 13, 2010 3:34 PM
  • Hi Lydia,

     

    I am writing to check the status of the issue on your side.  Would you mind letting me know the result of the suggestions? 

     

    If you need further assistance, please feel free to let me know.   I will be more than happy to be of assistance.

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, May 17, 2010 1:54 AM