无法将字符串传递给存储过程

Can't pass a string to a stored procedure

这是我的存储过程...

alter PROCEDURE ReplyToEmailConfirmation 
    @uniqueKey varchar(36)
AS
BEGIN

Print 'Hello World!'
END

这是代码...

Set cmd = Server.CreateObject("ADODB.Command")
With cmd
    .ActiveConnection = getConfigValue("ASPClassicConnectionString")
    .CommandType = adCmdStoredProc
    .CommandText = "[ReplyToEmailConfirmation]"

    .Parameters.Append .CreateParameter("@uniqueKey", adVarChar, adParamInput, 36, "dc8d8bfd-ea3a-4ad9-9f2d-92831eb2655a")

End With

cmd.Execute

这是错误...

ADODB.Command error '800a0bb9'

Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.

我如何让它工作?目的是使用 adGUID,但我想我会尝试 adVarChar 来缩小错误范围。

如果你read the documentation for CreateParameter()一切就都清楚了;

If you specify a variable-length data type in the Type argument, you must either pass a Size argument or set the Size property of the Parameter object before appending it to the Parameters collection; otherwise, an error occurs.

当您传递 VARCHAR 时,它是 "可变长度" 数据类型,您必须在调用 [=11= 时指定 Size ].

Dim cmd

Set cmd = Server.CreateObject("ADODB.Command")
With cmd
    'Let the cmd deal with the connection.
    .ActiveConnection = getConfigValue("ASPClassicConnectionString")
    .CommandText = "[ReplyToEmailConfirmation]"
    .CommandType = adCmdStoredProc
    Call .Parameters.Append(.CreateParameter("@uniqueKey", adVarChar, adParamInput, 100))
    .Parameters("@uniqueKey") = "dc8d8bfd-ea3a-4ad9-9f2d-92831eb2655a"
End With
Call cmd.Execute()

'Tidy up memory
Set cmd = Nothing

还包括 adCmdStoredProcCommandType,它告诉 ADO 将此命令解释为存储过程调用,如果没有它,默认值为 adCmdUnknown,这意味着 ADO 必须尝试执行什么命令是,无论多么小都会增加不必要的开销。

也不热衷于实例化 ADODB.Connection 对象只是为了执行 ADO.Command 对象,这意味着您必须自己管理关闭 ADODB.Connection。相反,让 ADODB.Command 为你做这件事,通过传递一个连接字符串让它创建连接并自行销毁它。假设 getConfigValue("ASPClassicConnectionString") returns 一个连接字符串,您可以将它直接传递给 ActiveConnection 并且 ADODB.Command 将实例化连接并处理它。


有用的链接

  • A: Passing Parameters to a Stored Procedure using ASP (解释如何使用 METADATA 来包含命名常量)

我没有为 adCmdStoredProcadVarCharadGUID 常量包含 adovbs.inc。朵.