SQL Server 2005 从 Excel (VBA) 运行存储过程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7268830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 13:56:34  来源:igfitidea点击:

SQL Server 2005 running stored procedure from Excel (VBA)

sqlsql-serverexcelvbaado

提问by Waller

I am trying to run a stored procedure on my SQL server. I think the connection type is 'ODBC', but I am unsure, I am trying the below:

我正在尝试在我的 SQL 服务器上运行一个存储过程。我认为连接类型是“ODBC”,但我不确定,我正在尝试以下操作:

'Declare some variables
Dim cnn As ADODB.Connection
Dim cmd As ADODB.Command
Dim strSQL As String
Dim gway As String

'Create a new Connection object
Set cnn = New ADODB.Connection

'Set the connection string
cnn.ConnectionString = connectionString 'See http://connectionstrings.com if you need help on building this string for your database!

'Create a new Command object
Set cmd = New ADODB.Command
'Associate the command with the connection
'Open the Connection to the database
cnn.Open
cmd.ActiveConnection = cnn


VARIABLE= "param"
strSQL = "EXECUTE dbo.NAMEWASHERE '" & VARIABLE & "';"



cmd.Execute strSQL

'Close the connection again
cnn.Close

When I try to run this I recieve the error: Run-time error '3709';

当我尝试运行它时,我收到错误:运行时错误 '3709';

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

请求的操作需要 OLE DB 会话对象,当前提供程序不支持该对象。

I am sure I am just getting my connection type wrong somewhere, but could anybody help me out?

我确定我只是在某处弄错了我的连接类型,但有人可以帮我吗?

To confirm, I'm trying to connect via ODBC to an SQLServer2005, via Excel VBA (2003)

为了确认,我正在尝试通过 ODBC 连接到 SQLServer2005,通过 Excel VBA (2003)

Many thanks.

非常感谢。

UPDATE WITH FIX:You must open the connection before using ".ActiveConnection = """. I have editted the original code to show this change.

UPDATE WITH FIX:您必须在使用“.ActiveConnection =”“”之前打开连接。我已经编辑了原始代码以显示此更改。

采纳答案by Waller

UPDATE WITH FIX: You must open the connection before using ".ActiveConnection = """. I have editted the original code to show this change.

UPDATE WITH FIX:您必须在使用“.ActiveConnection =”“”之前打开连接。我已经编辑了原始代码以显示此更改。