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

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

问题描述:

我想在我的SQL服务器上运行存储过程。 我想连接类型是“ODBC”,但我不确定,我想下面:从Excel(VBA)运行存储过程的SQL Server 2005

'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会话对象,该对象不受当前提供者支持。

我相信我只是让我的连接类型错了某处,但有人能帮我吗?

为了确认,我想通过ODBC连接到SQLServer2005的,通过Excel的VBA(2003)

非常感谢。

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

+0

代码看起来不错,错误的原因取决于您使用的特定连接字符串,它是什么? –

+0

ODBC; DSN = serverAddr; UID = LoginName; PWD = MyPassword; LANGUAGE = us_english; DATABASE =数据库名。 显然这是敏感的,所以如果错误在这里,您可能无法提供帮助。我希望你能帮忙。 编辑:我使用SQL Express 2005如果这件事。 – Waller

+0

我将使用来自http://www.connectionstrings.com/sql-server-2005#p2的* SQL Server Native Client 10.0 OLE DB提供程序*连接字符串 –

更新与修复:您必须先打开连接,然后再使用“.ActiveConnection =”“”。我编辑了原始代码以显示此更改。