Параметризация sql в asp - PullRequest
       11

Параметризация sql в asp

0 голосов
/ 03 апреля 2020

Я не очень знаком с asp, но у меня есть один проект со страницей этого.

<% 
'declare the variables 
Dim Connection
Dim ConnString
Dim Recordset
Dim SQL
Dim userName 
userName = Request.Form("userName")
'define the connection string, specify database driver
ConnString="JJJJJ"//connection information

'declare the SQL statement that will query the database
SQL = "SELECT info AS Lunch, "
SQL = SQL & "FROM dbo.AgentActivityLog WHERE UserId = '" & userName & "'

'create an instance of the ADO connection and recordset objects
Set Connection = Server.CreateObject("ADODB.Connection")
Set Recordset = Server.CreateObject("ADODB.Recordset")

'Open the connection to the database
Connection.Open ConnString

'Open the recordset object executing the SQL statement and return records 
Recordset.Open SQL,Connection

'first of all determine whether there are any records 
If Recordset.EOF Then 
Response.Write("No records returned.") 
Else 
'process record
Next
Recordset.MoveNext     
Loop
End If

'close the connection and recordset objects to free up resources
Recordset.Close
Set Recordset=nothing
Connection.Close
Set Connection=nothing
%>

Как мне параметризировать имя пользователя? У меня нет способа отладки, я не знаю, как работает этот язык, поэтому все, что я пробовал, оказалось неудачным, и я понятия не имею, почему.

1 Ответ

1 голос
/ 03 апреля 2020

Я бы посоветовал вам взглянуть на ответ по следующей ссылке, так как он заполнит ответы, которые вы ищете: VBA, ADO.Connection и параметры запроса

Из предоставленного кода отсутствует критическая piece - это команда ADODB.Command для SQL, которая будет фактически запущена и для добавления параметра запрос вы бы:

    Set Param = Command.CreateParameter("@userid",adVarChar,adParamInput)
    Param.Value = userName
    Command.Paramters.Append Param
...