简体   繁体   中英

How can I “INNER JOIN” the results of two different queries from two different databases with VB.NET?

I connect to SQL Server database and grab a DataSet of the result with the following function:

Public Function GetDataSQL(ByVal queryString As String, ByVal addParameters As Action(Of SqlParameterCollection)) As DataTable
    Dim result As New DataTable()
    Using cn As New SqlConnection("server=.\sqlexpress;Integrated Security=SSPI; database=Pancakes"), _
    cmd As New SqlCommand(queryString, cn)
        addParameters(cmd.Parameters)
        cn.Open()
        Using rdr = cmd.ExecuteReader()
            result.Load(rdr)
        End Using
    End Using
    Return result
End Function

Then I do a virtually identical function to get the result of a query to an Access database.

How can I do an "INNER JOIN" on these two datasets? There is a "merge" method, but I don't think it does an "INNER JOIN"...

You could use OPENROWSET() within SQL Server to treat the Access database as if it were native data, and do the inner join there. See http://msdn.microsoft.com/en-us/library/ms190312.aspx for syntax details.

Use LINQ to DataSet. This way no matter where the 2 DataTables get the data - you can join them uniformally.

Since you're bringing your results back as DataTables, you will need to use something like LINQ to DataSets or set up associations between your DataTables in the DataSet. If you want to read up on LINQ to DataSets, checkout the free bonus chapter 14 at http://www.manning.com/marguerie/ .

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