以下Excel VBA代码运行良好:
Dim conn As ADODB.Connection
Set conn = New ADODB.Connection
With conn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = ";Data Source='" & _
ThisWorkbook.FullName & _
"';Extended Properties='Excel 12.0;HDR=Yes;IMEX=1';"
.Open
.Execute("select * from [ODBC;Driver={SQL Server};SERVER=MyServer\SqlInstance;DATABASE=DatabaseName;].dbo.Test")
End With
我现在已经在我的机器上添加了一个ODBC数据源名称并希望使用它,但以下操作失败:
Dim conn As ADODB.Connection
Set conn = New ADODB.Connection
With conn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = ";Data Source='" & _
ThisWorkbook.FullName & _
"';Extended Properties='Excel 12.0;HDR=Yes;IMEX=1';"
.Open
.Execute("select * from [ODBC;Driver={SQL Server};DSN=MyDsn;].dbo.Test")
End With
错误消息是" ODBC - 连接到' MyDsn'失败"