[英]Requested operation requires an OLE DB Session object… - Connecting Excel to SQL server via ADO
I'm attempting to take Excel 2003 and connect it to SQL Server 2000 to run a few dynamicly generated SQL Queries which ultimately filling certain cells. 我正在尝试使用Excel 2003并将其连接到SQL Server 2000以运行一些动态生成的SQL查询,这些查询最终会填充某些单元格。
I'm attempting to do this via VBA via ADO (I've tried 2.8 to 2.0) but I'm getting an error while setting the ActiveConnection
variable which is inside the ADODB.Connection
object. 我试图通过ADO通过VBA执行此操作(我已经尝试过2.8到2.0)但是在设置ADODB.Connection
对象内部的ActiveConnection
变量时出现错误。 I need to resolve this pretty quick... 我需要快速解决这个问题......
Requested operation requires an OLE DB Session object, which is not supported by the current provider. 请求的操作需要OLE DB会话对象,当前提供程序不支持该对象。
I'm honestly not sure what this error means and right now I don't care. 老实说,我不确定这个错误意味着什么,现在我不在乎。 How can get this connection to succeed so that I can run my queries? 如何才能使此连接成功,以便我可以运行查询?
Here is my VB code: 这是我的VB代码:
Dim SQL As String, RetValue As String
SQL = " select top 1 DateTimeValue from SrcTable where x='value' " 'Not the real SQL
RetValue = ""
Dim RS As ADODB.Recordset
Dim Con As New ADODB.Connection
Dim Cmd As New ADODB.Command
Con.ConnectionString = "Provider=sqloledb;DRIVER=SQL Server;Data Source=Server\Instance;Initial Catalog=MyDB_DC;User Id=<UserName>;Password=<Password>;"
Con.CommandTimeout = (60 * 30)
Set Cmd.ActiveConnection = Con ''Error occurs here.
' I'm not sure if the rest is right. I've just coded it. Can't get past the line above.
Cmd.CommandText = SQL
Cmd.CommandType = adCmdText
Con.Open
Set RS = Cmd.Execute()
If Not RS.EOF Then
RetValue = RS(0).Value
Debug.Print "RetValue is: " & RetValue
End If
Con.Close
I imagine something is wrong with the connection string but I've tried over a dozen variations. 我想连接字符串有问题,但我尝试了十几种变体。 Now I'm just shooting in the dark.... 现在我只是在黑暗中拍摄....
Note/Update : To make matters more confusing, if I Google for the error quote above, I get a lot of hits back but nothing seems relevant or I'm not sure what information is relevant.... 注意/更新 :为了让事情更加混乱,如果我谷歌上面的错误报价,我得到了很多回击,但似乎没有任何相关性,或者我不确定哪些信息是相关的....
I've got the VBA code in "Sheet1" under "Microsoft Excel Objects." 我在“Microsoft Excel Objects”下的“Sheet1”中获得了VBA代码。 I've done this before but usually put things in a module. 我以前做过这个,但通常把东西放在一个模块中。 Could this make a difference? 这会有所作为吗?
You have not opened your connection yet. 您尚未打开连接。 I think you need a Con.Open
before you assign it to the Command object. 我认为在将其分配给Command对象之前需要一个Con.Open
。
Con.ConnectionString = "Provider=sqloledb;DRIVER=SQL Server;Data Source=Server\Instance;Initial Catalog=MyDB_DC;User Id=<UserName>;Password=<Password>;"
Con.CommandTimeout = (60 * 30)
Con.Open
Set Cmd.ActiveConnection = Con 'Error occurs here.
Cmd.CommandText = SQL
Cmd.CommandType = adCmdText
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.