简体   繁体   中英

How can I join two datasets using a foreign key to create a new dataset?

I previously had a external linked table in my access database. The code worked fine.

But I think it would be better to define the links using code instead.

So now I have separated the sql for each table to populate each dataset.

How can I JOIN two Datasets using a foreign key to create a new combined dataset table?

Below I have copied my previous SQL and an attempt at creating a new combined dataset. But I don't know what to do from here... please can you help?

THIS IS THE ORIGINAL CODE

        ' Querying data from database
        Using oConn As New OleDbConnection(strConnOrders)

            oConn.Open()

            ' First Select the supplier details best on the specified Preferred Supplier Foreign Key in tblOrderDetails.

            Dim sSQL As String

            sSQL = "SELECT O.fldBarcode, O.fldProductCode, O.fldProductName, O.fldPackSize, O.fldOrderQty, " & vbCrLf & _
                   "O.fldOrderFK, O.fldPrefSupplierFK, S.fldID,IIF(O.fldPrefSupplierFK IS NULL OR O.fldPrefSupplierFK = 0 " & vbCrLf & _
                   "OR O.fldPrefSupplierFK = 34 OR O.fldPrefSupplierFK = 1, 'Unspecified Supplier',S.fldSupplierName) AS fldSupplierName, S.fldSupplierAddress, " & vbCrLf & _
                   "S.fldSupplierPostCode, S.fldSupplierPhone, S.fldSupplierFax, S.fldSupplierEmail, S.fldSupplierWeb, S.fldContactName, " & vbCrLf & _
                   "IIF(S.fldAccountNo IS NULL, 'NA', S.fldAccountNo) AS fldAccountNo " & vbCrLf & _
                   "FROM tblOrderDetails O LEFT OUTER JOIN tblSuppliers S ON O.fldPrefSupplierFK = S.fldID " & vbCrLf & _
                   "WHERE (O.fldSelect = True);"

            Using da As New OleDbDataAdapter(sSQL, oConn)
                da.Fill(ds, "Report1Data")
            End Using

        End Using

THIS IS THE NEW TEST CODE

        ' THE DATASETS ARE FILLED SEPERATELY THIS TIME
        Dim dsSuppliersTable As New DataSet
        Dim dsOrdersTable As New DataSet
        Dim dsCombinedTable As New DataSet
        Dim dsReportData As New DataSet

        Dim strSqlSupplierFields As String = "SELECT S.fldID, fldSupplierName, S.fldSupplierAddress, S.fldSupplierPostCode, " & _
                                             "S.fldSupplierPhone, S.fldSupplierFax, S.fldSupplierEmail, S.fldSupplierWeb, " & _
                                             "S.fldContactName FROM tblSuppliers;"

        dsSuppliersTable = getDatasetTable(strConnSuppliers, strSqlSupplierFields, "tblSuppliers")  

        Dim strSqlOrderFields As String = "SELECT O.fldBarcode, O.fldProductCode, O.fldProductName, O.fldPackSize, O.fldOrderQty, " & _
                                          "IIF(O.fldPrefSupplierFK IS NULL OR O.fldPrefSupplierFK = 0, S.fldID, " & _
                                          "OR O.fldPrefSupplierFK = 1, 'Unspecified Supplier', S.fldSupplierName) AS fldSupplierName, " & _
                                          "O.fldOrderFK, O.fldPrefSupplierFK FROM tblOrderDetails WHERE (O.fldSelect = True);"

        dsOrdersTable = getDatasetTable(strConnOrders, strSqlOrderFields, "tblOrders")


        If dsOrdersTable.Tables(0).Rows.Count > 0 Then

            ' MERGE THE DATA FROM dsOrdersTable and dsSuppliersTable
            ' LINK THEM TOGETHER USING THE FOREIGN KEY

            dsCombinedTable.Tables("Report1Data") ' I'M NOT SURE WHAT TO DO HERE TO JOIN THE DATASETS TOGETHER

            ' ALSO THE IIF STATEMENT IN strSqlOrderFields IS WRONG NOW, BECAUSE IT USED TO LINK TO THE SUPPLIERS TABLE
            ' HOW CAN I SOLVE THIS?

            ' THEN SAVE THE NEW COMBINED DATASET INTO dsReportData,
            dsReportData.Tables.Add(dsCombinedTable.Tables("Report1Data"))

        End If

        ' THIS IS THE END OF NEW TEST CODE *****************************************************************************

In most cases thinking

it would be better to define the links using code instead.

...would be wrong. Databases are optimised to do this sort of thing in many many ways. You're just going to have to write code to create the third dataset yourself, and I bet the code you write won't be as efficient as a database can do it.

Use a Join. They're what databases are for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM