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

时间:2016-03-22 12:15:50

标签: sql-server vbscript asp-classic ado named-parameters

我正在使用此查询从经典的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

1 个答案:

答案 0 :(得分:3)

问题在于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 = ?"

如果对您的查询you are wanting to pass named parameters,您没有太多选择,但这个小技巧很有用。

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"

有用的链接