【问题标题】:Trouble calling SQL Server Stored proc from Access 2007从 Access 2007 调用 SQL Server 存储过程时遇到问题
【发布时间】:2013-09-07 07:30:10
【问题描述】:

我在 SQL Server (2012) 中有一个 SP。它返回单行...使用 SSMS 验证

exec dbo.cpas_DeleteProjects N'3555,3565'

它返回一个有效的行,列中包含记录计数信息。

从 Access(2007) 开始,我使用 adodb 调用来调用它:

Sub DevolveProjects(ProjectIDs As String)
''
'   Looking for a comma-delimited list of project IDs in string form: "34,52,14"
'

Dim cnn As New adodb.Connection
Dim rstReturnValues As adodb.Recordset

cnn.ConnectionString = "Provider=SQLOLEDB;Server=" & CurrentServerName() & ";Initial Catalog=" & CurrentDBName() & ";Integrated Security=SSPI;"
cnn.Open
Set rstReturnValues = cnn.Execute("exec dbo.cpas_DeleteProjects N'" & ProjectIDs & "'")

With rstReturnValues...

问题是,rstReturnValues 已关闭。没有数据。

我已使用 SQL Profiler 验证,当从上述 Access 应用程序调用时 sp 正在运行预期的 SQL,并且似乎执行得很好,因此身份验证没有问题。我只是没有填充我的记录集。当我将 SQL Profiler 看到的确切 SQL 语句复制并粘贴到 SSMS 查询中时,它返回的正是我所期望的......

我是否对 .execute 调用进行了错误编码?想法?

【问题讨论】:

  • 看起来不错,你是如何查询记录集的?
  • “With”子句的断点显示 rstReturnValues.state=0

标签: sql sql-server stored-procedures ms-access-2007


【解决方案1】:

代码看起来不错,尽管您可以尝试使用绑定参数而不是单个 SQL 字符串。对于快速而肮脏的东西,它没有任何区别,但这消除了 SQL 注入的可能性

Dim lConnection as new ADODB.Connection
Dim lCommand as new ADODB.Command
Dim lParameter as ADODB.Parameter
Dim lRecordset as ADODB.Recordset

lConnection.ConnectionString = "Provider=SQLOLEDB;Server=" & CurrentServerName() & ";Initial Catalog=" & CurrentDBName() & ";Integrated Security=SSPI;"
lConnection.Open
lCommand.ActiveConnection = lConnection
lCommand.CommandText = "dbo.cpas_DeleteProjects"
lCommand.CommandType = adCmdStoredProc
set lParameter = lCommand.CreateParameter(, adVarWChar, adParamInput, 200, ProjectIDs) 'replace 200 with parameter length, different syntax if nvarchar(max)
lCommand.Parameters.Append lParameter 
Set lRecordset = lCommand.Execute()

Debug.Print lRecordset(0) 'Does the recordset contain anything?

...
lRecordset.Close
lConnection.Close

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-24
  • 1970-01-01
  • 2013-09-18
  • 2012-03-28
  • 1970-01-01
  • 2011-05-15
  • 1970-01-01
相关资源
最近更新 更多