在绝对紧急的情况下,我正在尝试浏览我的网站并添加参数化查询。我是新手,刚刚了解它们。
我的问题是,我对连接类型知之甚少,并且我看到的所有示例都使用另一种连接方法,这让我感到困惑。我并不是特别想改变连接到数据库的方式,因为它位于很多页面上,我只是想更新我的查询以使其更安全。
这就是我连接到数据库的方式:
Set connContent = Server.CreateObject("ADODB.Connection")
connContent.ConnectionString = "...blah...blah...blah..."
connContent.Open
这是带参数的 SQL 位:
username = Trim(Request("username"))
connContent.Prepared = True
Const ad_nVarChar = 202
Const ad_ParamInput = 1
SQL = " SELECT * FROM users WHERE (username=?) ; "
Set newParameter = connContent.CreateParameter("@username", ad_nVarChar, adParamInput, 20, username)
connContent.Parameters.Append newParameter
Set rs = connContent.Execute(SQL)
If NOT rs.EOF Then
' Do something...
End If
rs.Close
它显然不起作用,但我需要知道我是否真的可以使用我拥有的连接来实现这一目标,或者我是否完全错过了阻止它工作的东西?
在我继续用接下来的两天时间调试我不熟悉的东西之前,我想知道我至少走在正确的轨道上......
第二个片段中的代码是正确的,但应该应用于新的ADODB.Command
对象,而不是Connection
object:
username = Trim(Request("username"))
'-----Added this-----
Dim cmdContent
Set cmdContent = Server.CreateObject("ADODB.Command")
' Use this line to associate the Command with your previously opened connection
Set cmdContent.ActiveConnection = connContent
'--------------------
cmdContent.Prepared = True
Const ad_nVarChar = 202
Const ad_ParamInput = 1
SQL = " SELECT * FROM users WHERE (username=?) ; "
Set newParameter = cmdContent.CreateParameter("@username", ad_nVarChar, ad_ParamInput, 20, username)
cmdContent.Parameters.Append newParameter
cmdContent.CommandText = SQL
Set rs = cmdContent.Execute
If NOT rs.EOF Then
' Do something...
End If
rs.Close
顺便说一句,拼写错误adParamInput
代替ad_ParamInput
(在我的示例中已更正)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)