ADO 参数化查询不返回任何结果

ADO parameterised query not returning any result

我正在使用此查询从经典 asp 页面中的 vbscript 获取一些结果,但它没有返回任何值。该页面是空白的,也没有错误。有什么建议吗?

dim cmd, admin_no
admin_no = request.QueryString("admin")

set cmd = server.CreateObject("ADODB.Command")
cmd.ActiveConnection = con
sql = ""
sql = sql & "DECLARE @admin_no int;"
sql = sql & "SELECT field1, field2 FROM mydb.mytable where admin_no = @admin_no"
cmd.CommandText = sql
cmd.CommandType = adCmdText
cmd.Parameters.Append cmd.CreateParameter("@admin_no", adInteger, adParamInput, , admin_no)

'response.write(cmd.Parameters(0))
'response.write(cmd.CommandText)

set rs = server.CreateObject("ADODB.Recordset") 
rs.Open cmd

这里的问题是 adCmdText 使用 ? 占位符来传递参数,更改行

sql = sql & "SELECT field1, field2 FROM mydb.mytable where admin_no = @admin_no"

sql = sql & "SELECT field1, field2 FROM mydb.mytable where admin_no = ?"

如果 您的查询没有太多选项,但这个小技巧很有用。

sql = ""
sql = sql & "DECLARE @admin_no int;"
sql = sql & "SET @admin_no = ?;"
sql = sql & "SELECT field1, field2 FROM mydb.mytable where admin_no = @admin_no"

有用的链接

  • Using Stored Procedure in Classical ASP .. execute and get results
  • ASP parameters to call a stored procedure in SQL Server
  • Change stored proc to inline sql