Search code examples
vb.netdatatabledatasetoledb

.NET : Combine 2 DataSet/DataTable in a single DataTable


I have 2 Datasets with 1 table (7 cols) in each. Both the tables have different column names, but same structure. I want to join/combine/merge these 2 tables and produce a single table with 7 cols only. I treid with Merge, but that produces 14 cols & doesn't actually join both of them. I searched in this site and others too, but couldn't find any appropriate solution for my case. My code is :

    Private Sub FillMatchToOtstdAdapter()
    Dim sql1 As String = "SELECT DOCMCH.docmch_nAccNo, DOCMCH.docmch_lRefDocId, DOCMCH.docmch_nDocLnNo, DOCMCH.docmch_dAdjustedAmt, DOCMCH.docmch_dAdjustedAmt, DOCHDR.dochdr_nVersionNo, 'D' AS Expr1 FROM TXNTYP INNER JOIN (DOCHDR INNER JOIN DOCMCH ON DOCHDR.dochdr_lDocId = DOCMCH.docmch_lRefDocId) ON TXNTYP.txntyp_sDocTyp = DOCHDR.dochdr_sDocType WHERE (((TXNTYP.txntyp_cDocTypCat)='F') AND ((DOCHDR.dochdr_lDocDt)>=20120630));"
    Dim sql2 As String = "select otstnd_nAccNo, otstnd_lDocId, otstnd_nDocLnNo, otstnd_dOtstndgAmt, otstnd_dDocLnAmt, otstnd_nVersionNo, otstnd_cOtstndgSign from otstnd"

    ' Get Records of Query 1
    ds_otstnd_Adap = New OleDb.OleDbDataAdapter(sql1, connStr)
    ds_otstnd_Adap.Fill(ds_otstnd_Ds)
    ds_otstnd_Dt = ds_otstnd_Ds.Tables(0)

    ' Get Records of Query 2
    Dim adap As New OleDb.OleDbDataAdapter(sql2, connStr)
    Dim ds As New DataSet
    adap.Fill(ds)

    ' Merge both of them
    ds_otstnd_Dt.Merge(ds.Tables(0), False, MissingSchemaAction.Add)
    DataGridView1.DataSource = ds_otstnd_Dt
End Sub

Any idea how do I produce the results in a single table with 7 cols only - just data from 2nd table to be added in 1st table. I was thinking of picking each Row from table2 and just addin table1, but their may be many rows & don't want to go in unnecessary process ifthings can be managed in easier and simple manner.

Any help is highly appreciated.

Thanks


Solution

  • I got it. I added " as Col1" to both the query's making same col for the results of each table. For merge, replaced MissingSchemaAction.Add to MissingSchemaAction.Ignore.

    This worked out for me.