有什么方法可以设置我从CursorType获得的ADODB.RecordSetADODB.Command.Execute吗?

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

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

关于sql - 使用ADODB.Command.Execute设置CursorType,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40134395/

10-10 07:32