2012-09-25 36 views
0

我是一个试图做一个非常简单的页面的ASP初学者。其功能是接受来自用户的两个输入并显示基于下一页上的报告。该报告的数据是在ASP页面上的SQL查询中获取的。如何将变量传递给ASP查询

这是我做了什么至今:

Set objConn = Server.CreateObject("ADODB.Connection") 
objConn.Open "Provider=MSDAORA; 
         Data Source=şemam; 
         User Id=xyz; 
         Password=xyz;" 
aranan = Request("aranan") 

Set objRs = objConn.Execute("select * from my_department where user_id = <input from user>") 
if objRs.BOF and objRs.eof then 
    response.end 
end if 

我现在面临的问题是,我无法找到如何正确地传递用户输入查询。

请帮忙!

回答

2

使用?作为占位符,然后将参数传递到Execute method

dim paramArray(0) 
paramArray(0) = 123 
Set objRs = objConn.Execute("select * from my_department where user_id = ?", paramArray) 
+0

+1因为我一直在使用经典的ASP永远不知道你可以通过这种方式传递参数。 – johna

1

要将参数发送到数据库查询,您需要使用命令对象。

Set objConn = Server.CreateObject("ADODB.Connection") 
objConn.Open "Provider=MSDAORA;" & _ 
      "Data Source=şemam;" & _ 
      "User Id=xyz;" & _ 
      "Password=xyz;" 
aranan = Request("aranan") 

Set objCmd = Server.CreateObject("ADODB.Command") 
Set objCmd.ActiveConnection = objConn 
objCmd.CommandText = "SELECT * FROM my_department WHERE user_id = ?" 
objCmd.CommandType = 1 

Set objRs = objCmd.Execute(, array(aranan)) 
if not objRs.EOF then 
' Do whatever you need to with the result... 
end if 
objRs.Close 
objConn.Close 

你关闭了连接,否则你最终会耗尽你的连接池之前,不要结束的响应。