簡體   English   中英

請求的操作需要OLE DB會話對象... - 通過ADO將Excel連接到SQL服務器

[英]Requested operation requires an OLE DB Session object… - Connecting Excel to SQL server via ADO

我正在嘗試使用Excel 2003並將其連接到SQL Server 2000以運行一些動態生成的SQL查詢,這些查詢最終會填充某些單元格。

我試圖通過ADO通過VBA執行此操作(我已經嘗試過2.8到2.0)但是在設置ADODB.Connection對象內部的ActiveConnection變量時出現錯誤。 我需要快速解決這個問題......

請求的操作需要OLE DB會話對象,當前提供程序不支持該對象。

老實說,我不確定這個錯誤意味着什么,現在我不在乎。 如何才能使此連接成功,以便我可以運行查詢?

這是我的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

我想連接字符串有問題,但我嘗試了十幾種變體。 現在我只是在黑暗中拍攝....

注意/更新 :為了讓事情更加混亂,如果我谷歌上面的錯誤報價,我得到了很多回擊,但似乎沒有任何相關性,或者我不確定哪些信息是相關的....

我在“Microsoft Excel Objects”下的“Sheet1”中獲得了VBA代碼。 我以前做過這個,但通常把東西放在一個模塊中。 這會有所作為嗎?

您尚未打開連接。 我認為在將其分配給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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM