Excel VBA - 运行SQL存储过程错误

时间:2017-07-19 10:38:48

标签: sql excel-vba adodb vba excel

我在Excel工作簿中使用this page.

复制了以下代码

代码:

Sub Button1_Click()

Dim con As ADODB.Connection
Dim cmd As ADODB.Command
Dim rs As ADODB.Recordset
Dim WSP1 As Worksheet

Set con = New ADODB.Connection
Set cmd = New ADODB.Command
Set rs = New ADODB.Recordset

'''Clear extract area'''
Worksheets("Extract").UsedRange.Delete

'''Log into SQL Server'''
con.Open "Provider = SQLOLEDB;" & _
         "Data Source = MySource;" & _
         "Initial Catalog = MyDB;" & _
         "User ID = MyID;" & _
         "Password = MyPassword;"
cmd.ActiveConnection = con

'''Set up parameters for stored procedure'''
cmd.Parameters.Append cmd.CreateParameter("startDate", adDate, adParamInput, , Range("C2"))
cmd.Parameters.Append cmd.CreateParameter("endDate", adDate, adParamInput, , Range("C3"))

cmd.CommandText = "DB.StoredProc"
Set rs = cmd.Execute(, , adCmdStoredProc)

Set WSP1 = Worksheets("Extract")
WSP1.Activate
If rs.EOF = False Then WSP1.Cells(1, 1).CopyFromRecordset rs

rs.Close
Set rs = Nothing
Set cmd = Nothing

con.Close
Set con = Nothing

End Sub

我在“如果rs.EOF = False然后”

行上收到以下错误消息
  

“关闭对象时不允许操作。”

这是我第一次使用这些功能。我做错了什么?

另外,我是否正确设置了多个参数?

---快速编辑--- 不确定是否值得一提的是我将日期格式化为yyyy-mm-dd,就像在SQL服务器中一样。

3 个答案:

答案 0 :(得分:1)

我通过将$(document).ready(function() { $('.submit').on("click",function() { $.post(this.id+".php", { id_shirt: $('#id_shirt').val(), name_shirt: $('#name_shirt').val(), price_shirt: $('#price_shirt').val() }, function(data) { $('#form_shirt .form-control').val(""); $('#table_shirt').html(data); }); }); }); 添加到我的存储过程来解决了这个问题。

我最初不确定我是否有权进行更改。我有一个新问题。如果我只使用一个参数,但是当我使用多个参数时,它可以工作。

我将关闭这个帖子,做一些研究,并可能开一个新的。

感谢您的帮助。

答案 1 :(得分:0)

 cmd.CommandText = "DB.StoredProc"

应该是存储过程的实际名称 - 除非您实际上将其命名为StoredProc?

答案 2 :(得分:0)

以下是您可以考虑的两个选项。

Option Explicit

Sub RunSProc()

'USE [Northwind]
'GO
'DECLARE @return_value int
'EXEC    @return_value = [dbo].[TestNewProc]
'        @ShipCountry = NULL
'SELECT  'Return Value' = @return_value
'GO

Dim con As Connection
Dim rst As Recordset
Dim strConn As String

Set con = New Connection
strConn = "Provider=SQLOLEDB;"
strConn = strConn & "Data Source=YOUR_SERVER_NAME;"
strConn = strConn & "Initial Catalog=YOUR_DB_NAME;"
strConn = strConn & "Integrated Security=SSPI;"

con.Open strConn

'Put a country name in Cell E1
Set rst = con.Execute("Exec dbo.TestNewProc '" & ActiveSheet.Range("E1").Text & "'")

'The total count of records is returned to Cell A5
ActiveSheet.Range("A5").CopyFromRecordset rst

rst.Close
con.Close

End Sub


Sub TryThis()

'USE [Northwind]
'GO
'DECLARE @return_value int
'EXEC    @return_value = [dbo].[Ten Most Expensive Products]
'SELECT  'Return Value' = @return_value
'GO

Dim con As Connection
Dim rst As Recordset

Set con = New Connection
con.Open "Provider=SQLOLEDB;Data Source=YOUR_SERVER_NAME;Initial Catalog=YOUR_DB_NAME;Integrated Security=SSPI;"

Set rst = con.Execute("Exec dbo.[Ten Most Expensive Products]")
'Results of SProc are returned to Cell A1
ActiveSheet.Range("A1").CopyFromRecordset rst

rst.Close
con.Close
End Sub