【问题标题】:Setting CursorType with ADODB.Command.Execute使用 ADODB.Command.Execute 设置 CursorType
【发布时间】:2021-01-17 09:51:26
【问题描述】:

有什么方法可以将CursorType 设置为我从ADODB.Command.Execute 获得的ADODB.RecordSet

我知道如果我这样做是有可能的:

rs = Server.CreateObject("ADODB.RecordSet")
rs.Open(cmd)

但是,我目前将Command.ExecuteParameters 参数一起使用,它会自动处理? 参数的变量数组以进行安全插值。因此使用RecordSet.Open 似乎不是一种选择。

具体来说,我的代码目前看起来像:

function ExecuteSQL(conn, sql, args)
    set ExecuteSQL_CmdObj = Server.CreateObject("ADODB.Command")
    ExecuteSQL_CmdObj.CommandType = adCmdText
    ExecuteSQL_CmdObj.CommandText = sql
    ExecuteSQL_CmdObj.ActiveConnection = conn
    if Ubound(args) = -1 then
        set ExecuteSQL = ExecuteSQL_CmdObj.Execute
    else
        set ExecuteSQL = ExecuteSQL_CmdObj.Execute(,args)
    end if
end function

如果我想维护同样的 API,但控制CursorType,如何实现?

【问题讨论】:

  • 这个问题的另一种表述是:有没有办法用RecordSet.Open自动处理?参数的变体数组?

标签: sql vbscript asp-classic sql-injection adodb


【解决方案1】:

据我所知,答案是ADODB.Command.Execute 是不可能的,但ADODB.RecordSet.Open 使用ADODB.Command.Parameters 是可能的:

function CreateSQLParameter(arg)
    set param = Server.CreateObject("ADODB.Parameter")

    select TypeName(arg)
        case "String"
            param.Type = adVarChar
            param.Size = Len(CStr(arg))
            param.Value = CStr(arg)
        case "Integer"
            param.Type = adInteger
            param.Value = CLng(arg)
        case "Double"
            param.Type = adDouble
            param.Value = CDbl(arg)
        case else
            ' 13 is the "Type Mismatch" error code
            Err.Raise(13,,, "Type '" & TypeName(arg) "' is not handled. Please add support for it to CreateSQLParameter")
    end select

    set CreateSQLParameter = param
end function

function CreateSQLCommand(sql, args)
    set cmd = Server.CreateObject("ADODB.Command")
    'From http://www.w3schools.com/asp/prop_comm_commandtype.asp.
    'adCmdText is for some reason undefined in our scope.
    cmd.CommandType = 1
    cmd.CommandText = sql

    for i = Lbound(args) to Ubound(args)
        set param = CreateSQLParameter(args(i))
        cmd.Parameters.Append(param)
    next

    set CreateSQLCommand = cmd
end function

function ExecuteSQL(conn, sql, args)
    set cmd = CreateSQLCommand(sql, args)
    set rs = Server.CreateObject("ADODB.RecordSet")
    rs.Open(cmd, conn)

    set ExecuteSQL = rs
end function

【讨论】:

  • 这里的CursorType设置在哪里?
  • 命名常量未定义,因为 VBScript 不知道 ADO 的类型库,您必须告诉它 - 请参阅 A: Passing Parameters to a Stored Procedure using ASP(关于 METADATA 的部分)。。跨度>
  • 不需要那种程度的封装。如果您在ADODB.Command 的上下文中设置使用.Append(.CreateParameter(...)) 添加参数的命令,并将所需的值传递给它并使用rs.Open() 执行,那么它的工作方式完全相同。然而,问题就像@ansgar-wiecherspointed out already,即使你设置了CursorType,它也会被忽略。
  • 光标类型在哪里?
【解决方案2】:

这里有一个快速简便的方法来完成此操作:


    Dim arrErrorCode(1,1)
    Dim ArrayRS
    
    On Error Resume Next
                
    Set rsGetIPInfo = Server.CreateObject("ADODB.Recordset")
    Set oCMD = Server.CreateObject("ADODB.Command")
                
    sSQL = "SELECT * FROM RemoteIPInfo WHERE RemoteIP_ID = ?"
                
    oCMD.ActiveConnection = oConnGlobal
    oCMD.CommandText = sSQL
    oCMD.CommandType = adCmdText
    oCMD.CommandTimeout = 120
    oCMD.Parameters.Append oCMD.CreateParameter("@RemoteIP_ID", adVarChar, adParamInput, ,RemoteIP_ID)
    
    rsGetIPInfo.CursorLocation = adUseClient
    rsGetIPInfo.Open oCMD, ,adOpenStatic, adLockReadOnly
    GetIPInfoCount = rsGetIPInfo.RecordCount
    
    If Not rsGetIPInfo.BOF And Not rsGetIPInfo.EOF Then
        ArrayRS = rsGetIPInfo.GetRows()
    End If
                
    arrRowNumberIPInfo = Ubound(ArrayRS, 1)  
                
    If Err.Number > 0 Then 
        arrRowNumberErrorCode = Ubound(arrErrorCode, 1)  
        Response.Write("Error Number: ") & Err.Number & "<br>"
        Response.Write("Error Description: ") & Err.Description & "<br>"
        Response.Write("Error Source: ") & Err.Source & "<br>"
        Err.Raise 13
        Response.End
                    
    End If
    
    rsGetIPInfo.Close
    Set rsGetIPInfo = Nothing
    
    On Error Goto 0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-28
    • 1970-01-01
    • 1970-01-01
    • 2012-02-08
    • 2011-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多