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

标签 sql sql-server excel vba ado

我正在尝试在我的 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

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

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

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

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

非常感谢。

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

最佳答案

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

关于SQL Server 2005 从 Excel (VBA) 运行存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7268830/

相关文章:

Excel 在一行中打印特定数字,受其他列的影响

php - 为什么要使用 num_rows

SQL Server 在现有选择列中添加计数列

mysql - Impossible MySQL insert ... select foreign key constraint error‽

c# - 动态翻译以避免 C# 语法错误

sql-server - SQL Server : combining multiple rows into one row

excel - 无论 NumberFormat 是否设置为 General,公式都显示为文本

excel - 使用cmd运行vbs脚本

java - SQLite 查询完全匹配

sql - 使用 Entity Framework 4.1 for Oracle 时,长列名会产生错误