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