使用 ADODB.Command.Execute 设置 CursorType

Setting CursorType with ADODB.Command.Execute

有没有办法为我从 ADODB.Command.Execute 获得的 ADODB.RecordSet 设置 CursorType

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

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,这如何实现?

据我所知,答案是 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

这里有一个快速简单的方法来完成这个:


    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