在VB.Net中加入多对多DataTable

时间:2015-02-24 19:40:58

标签: vb.net datatable

我有来自不同来源类型的2个数据源,例如:

Dim connSql As String = "Data Source=sql_src;Initial Catalog=my_db;Persist Security Info=True;User ID=usr;Password=pwd;"
Dim connOle As String = "Provider=IBMDA400;Data source=src;User Id=usr;Password=pwd"

dim qrySql as String = "Select uniqueID, name From tblSql"
dim qryOle as String = "Select name, ID From tblOle"

我使用这样的函数将它们放入DataTable s(主要用于参考):

Public Shared Function sqlQryDT(qry As String, conn As String) As DataTable
    Dim dt As New DataTable
    Dim sqlconn As New SqlClient.SqlConnection(conn)
    sqlconn.Open()
    Dim adapter As New SqlClient.SqlDataAdapter(qry, sqlconn)
    Try
        adapter.Fill(dt)
    Catch ex As Exception
    Finally
        sqlconn.Close()
    End Try
    Return dt
End Function

Public Shared Function oleQryDT(qry As String, conn As String) As DataTable
    Dim dt As New DataTable
    Dim cn As New OleDb.OleDbConnection(conn)
    Dim cmd As New OleDb.OleDbCommand("", cn)
    Dim da As New OleDb.OleDbDataAdapter(cmd)
    cmd.CommandText = qry
    Try
        cn.Open()
        da.Fill(dt)
    Catch ex As Exception
    Finally
        cn.Close()
    End Try
    Return dt
End Function

Dim dtSql as DataTable = sqlQryDT(qrySql, connSQL)
Dim dtOle as DataTable = oleQryDT(qryOle , connOle)

我的目标是让DataTableuniqueID,name,ID

组成

(编辑)如果我可以进行SQL查询,它看起来像SELECT * FROM dtSql JOIN dtOle ON dtSql.name = dtOle.name

我尝试使用DataRelation将表放入DataSet并在name上添加关系,但由于名称可能不唯一,因此失败。

我也尝试过使用LINQ,但总是返回IEnumerable(System.Collections.Generic),我找不到一种简单的方法将内容放入DataTable,除非迭代每个项目。并且,当存在大量列时代码很乱。

我觉得应该有一种方法可以在不使用For Each循环

的情况下执行此操作

我看了这些问题,但他们并没有完全帮助我。

How to inner join two already filled DataTables in VB.NET

Merging 2 data tables in vb.net

0 个答案:

没有答案