加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

sql-server – 请求的操作需要OLE DB会话对象… – 通过ADO将Exc

发布时间:2020-12-12 08:38:19 所属栏目:MsSql教程 来源:网络整理
导读:我正在尝试使用Excel 2003并将其连接到SQL Server 2000以运行一些动态生成的SQL查询,这些查询最终会填充某些单元格. 我试图通过ADO通过VBA执行此操作(我已经尝试过2.8到2.0)但是在设置ADODB.Connection对象内部的ActiveConnection变量时出现错误.我需要快速解
我正在尝试使用Excel 2003并将其连接到SQL Server 2000以运行一些动态生成的SQL查询,这些查询最终会填充某些单元格.

我试图通过ADO通过VBA执行此操作(我已经尝试过2.8到2.0)但是在设置ADODB.Connection对象内部的ActiveConnection变量时出现错误.我需要快速解决这个问题……

Requested operation requires an OLE DB Session object,which is not supported by the current provider.

老实说,我不确定这个错误意味着什么,现在我不在乎.如何才能使此连接成功,以便我可以运行查询?

这是我的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=ServerInstance;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

我想连接字符串有问题,但我尝试了十几种变体.现在我只是在黑暗中拍摄….

注意/更新:为了让事情更加混乱,如果我谷歌上面的错误报价,我得到了很多回击,但似乎没有任何相关性,或者我不确定哪些信息是相关的….

我在“Microsoft Excel Objects”下的“Sheet1”中获得了VBA代码.我以前做过这个,但通常把东西放在一个模块中.这会有所作为吗?

解决方法

您尚未打开连接.我认为在将其分配给Command对象之前需要一个Con.Open.
Con.ConnectionString = "Provider=sqloledb;DRIVER=SQL Server;Data Source=ServerInstance;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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读