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

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

I am using this query to get some results from my vbscript in a classic asp page and It does not come back with any values. The page is blank and there are no errors too. Any suggestion please?

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 使用 ? 占位符传递参数,换行

Problem here is adCmdText uses ? placeholder for passing parameters, change the line

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

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


如果您想要将命名参数传递给您的查询没有太多选择,但这个小技巧很有用.


If you are wanting to pass named parameters to your query you don't have too many options but this little trick is useful.

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"


有用的链接

  • 在经典 ASP 中使用存储过程 .. 执行并获得结果
  • 在 SQL Server 中调用存储过程的 ASP 参数
  • 将存储过程更改为内联sql

相关文章