我有来自不同来源类型的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)
我的目标是让DataTable
由uniqueID,name,ID
(编辑)如果我可以进行SQL查询,它看起来像SELECT * FROM dtSql JOIN dtOle ON dtSql.name = dtOle.name
我尝试使用DataRelation
将表放入DataSet
并在name
上添加关系,但由于名称可能不唯一,因此失败。
我也尝试过使用LINQ
,但总是返回IEnumerable(System.Collections.Generic)
,我找不到一种简单的方法将内容放入DataTable,除非迭代每个项目。并且,当存在大量列时代码很乱。
我觉得应该有一种方法可以在不使用For Each
循环
我看了这些问题,但他们并没有完全帮助我。