我正在尝试运行此命令
SELECT *
FROM [Sheet1$] AS A INNER JOIN [Orders$] IN'C:\Users\reddy\Desktop\Orders.xlsx' 'Excel 8.0;'
AS B
ON A.ContactName = B.ContactName;
但我遇到错误
“内部:无法执行代码阶段,因为抛出了异常 代码阶段:填充:尚未使用SelectCommand.Connection属性 已初始化”
Dim oComm As New OleDbCommand
Dim oledbAdapter As new OleDbDataAdapter(SQL,moconnection)
Dim oDataSet As New DataSet
oledbAdapter.Fill(oDataSet)
oledbAdapter.Dispose()
但是我希望Command应该由该代码执行
答案 0 :(得分:0)
我不确定该联接,因此无法对其进行测试。对于Excel,.xlsx文件是数据库,每张工作表是一张表。我希望Sheet1和Orders都在同一个文件中。如果不是这样,您将需要通过2个连接和一个DataSet来以不同的方式进行处理,该DataSet可以容纳两个可以建立关系的DataTables。
Private Sub OPCode()
Dim dt As New DataTable
Dim strFileName As String = "C:\Users\reddy\Desktop\Orders.xlsx"
Dim strCon As String = $"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={strFileName};Extended Properties=""Excel 12.0;HDR=YES;"""
Using cn = New OleDbConnection(strCon)
Dim sql = "Select * FROM [Sheet1$] As A INNER JOIN [Orders$] AS B On A.ContactName = B.ContactName;"
Using cmd As New OleDbCommand(sql, cn)
cn.Open()
dt.Load(cmd.ExecuteReader)
End Using
End Using
DataGridView1.DataSource = dt
End Sub