简体   繁体   English

如何使用VB.NET从两个不同的数据库中“内部联接”两个不同查询的结果?

[英]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: 我连接到SQL Server数据库,并使用以下函数获取结果的数据集:

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. 然后,我执行了几乎相同的功能,以将查询结果获取到Access数据库。

How can I do an "INNER JOIN" on these two datasets? 如何在这两个数据集上进行“ INNER JOIN”? 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. 您可以在SQL Server中使用OPENROWSET()将Access数据库视为本地数据,然后在其中进行内部联接。 See http://msdn.microsoft.com/en-us/library/ms190312.aspx for syntax details. 有关语法的详细信息,请参见http://msdn.microsoft.com/zh-cn/library/ms190312.aspx

Use LINQ to DataSet. 使用LINQ进行数据设置。 This way no matter where the 2 DataTables get the data - you can join them uniformally. 这样,无论2个DataTable从何处获取数据,您都可以统一地将它们联接在一起。

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. 由于您要将结果作为DataTables带回来,因此您将需要使用LINQ之类的东西到DataSets或在DataSet中的DataTables之间建立关联。 If you want to read up on LINQ to DataSets, checkout the free bonus chapter 14 at http://www.manning.com/marguerie/ . 如果您想阅读LINQ to DataSets,请在http://www.manning.com/marguerie/查看免费的第14章奖金。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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