gpt4 book ai didi

MySQL/经典 ASP - 参数化查询

转载 作者:搜寻专家 更新时间:2023-10-30 20:12:12 24 4
gpt4 key购买 nike

在绝对紧急的情况下,我正在尝试浏览我的网站并添加参数化查询。我是新手,才刚刚了解它们。

我的问题是,我对连接类型知之甚少,而且我看到的所有示例都使用了另一种连接方法,这让我感到困惑。我并不是特别想改变我连接到我的数据库的方式,因为它在很多页面上,我只是想更新我的查询以更安全。

这就是我连接到我的数据库的方式:

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 对象:

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(在我的示例中已更正)。

关于MySQL/经典 ASP - 参数化查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7734673/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com