存储过程经典的asp

时间:2011-06-21 11:28:28

标签: sql stored-procedures parameters vbscript asp-classic

使用以下函数,如何使用带参数的存储过程。

我必须使用参数来调用存储过程以获得工作经验,但我似乎无法工作如何工作,通常我会在记录集参数中有一个command.execute然后遍历记录集来获取我的输出。

以下功能是我选择的几个缩小使用的功能。我使用记录集函数调用了存储过程。

记录

    function Recordset(sNewCommandText, iNewCommandType, iNewCommandTimeout, lng_RecordsetNumber)
            obj_Command.CommandText = sNewCommandText
            if IsNumeric(iNewCommandType) then
                obj_Command.CommandType = iNewCommandType
            else
                obj_Command.CommandType = 1
            end if
            if IsNumeric(iNewCommandTimeout) then
                obj_Command.CommandTimeout = iNewCommandTimeout
            else
                obj_Command.CommandTimeout = 3
            end if
            if RecordsetIsOpen(lng_RecordsetNumber) = 1 then
                CloseRecordset lng_RecordsetNumber
            end if
            select case lng_RecordsetNumber
                case 0
                case 1
                    obj_Recordset1.Open obj_Command
                case 2
                    obj_Recordset2.Open obj_Command
                case 3
                    obj_Recordset3.Open obj_Command
                case else
                    obj_ExtraRecSets.Item(lng_RecordsetNumber).Open obj_Command
            end select
        end function

执行

    function Execute(sNewCommandText, iNewCommandType, iNewCommandTimeout)
        Execute= 0
        obj_Command.CommandText = sNewCommandText
        if IsNumeric(iNewCommandType) then
            obj_Command.CommandType = iNewCommandType
        else
            obj_Command.CommandType = 1
        end if
        if IsNumeric(iNewCommandTimeout) then
            obj_Command.CommandTimeout = iNewCommandTimeout
        else
            obj_Command.CommandTimeout = 3
        end if
        obj_Command.Execute
        Execute= 1
    end function

获取字段

function GetField(FieldNumber, iFieldAttribute, lng_RecordsetNumber)
        dim lng_tmpcnt
        if RecordsetIsOpen(lng_RecordsetNumber) = 1 and IsNumeric(iFieldAttribute) and BOF(lng_RecordsetNumber) = 0 and EOF(lng_RecordsetNumber) = 0 then
            select case iFieldAttribute
                case 0
                    select case lng_RecordsetNumber
                        case 1
                            GetField = obj_Recordset1.Fields(FieldNumber).Type
                        case 2
                            GetField = obj_Recordset2.Fields(FieldNumber).Type
                        case 3
                            GetField = obj_Recordset3.Fields(FieldNumber).Type
                        case else
                            GetField = obj_ExtraRecSets.Item(lng_RecordsetNumber).Fields(FieldNumber).Type
                    end select
                case 1
                    select case lng_RecordsetNumber
                        case 1
                            GetField = obj_Recordset1.Fields(FieldNumber).Name
                        case 2
                            GetField = obj_Recordset2.Fields(FieldNumber).Name
                        case 3
                            GetField = obj_Recordset3.Fields(FieldNumber).Name
                        case else
                            GetField = obj_ExtraRecSets.Item(lng_RecordsetNumber).Fields(FieldNumber).Name
                    end select 
                case 2
                    select case lng_RecordsetNumber
                        case 1
                            GetField = obj_Recordset1.Fields(FieldNumber).Value
                        case 2
                            GetField = obj_Recordset2.Fields(FieldNumber).Value
                        case 3
                            GetField = obj_Recordset3.Fields(FieldNumber).Value
                        case else
                            GetField = obj_ExtraRecSets.Item(lng_RecordsetNumber).Fields(FieldNumber).Value
                    end select
                case 3
                    select case lng_RecordsetNumber
                        case 1
                            GetField = obj_Recordset1.Fields(FieldNumber).ActualSize
                        case 2
                            GetField = obj_Recordset2.Fields(FieldNumber).ActualSize
                        case 3
                            GetField = obj_Recordset3.Fields(FieldNumber).ActualSize
                        case else
                            GetField = obj_ExtraRecSets.Item(lng_RecordsetNumber).Fields(FieldNumber).ActualSize
                    end select
                case else
                    select case lng_RecordsetNumber
                        case 1
                            for lng_tmpcnt=0 to obj_Recordset1.Fields(FieldNumber).Properties.count-1
                                if lng_tmpcnt <> 15 and lng_tmpcnt <> 17 then
                                    GetField = GetField & lng_tmpcnt & " (" & obj_Recordset1.Fields(FieldNumber).Properties(lng_tmpcnt).name & "(" & obj_Recordset1.Fields(FieldNumber).Properties(lng_tmpcnt).type & ")= " & obj_Recordset1.Fields(FieldNumber).Properties(lng_tmpcnt).value & ")<br>" & vbcrlf
                                end if
                            next 
                        case 2
                            for lng_tmpcnt=0 to obj_Recordset2.Fields(FieldNumber).Properties.count-1
                                if lng_tmpcnt <> 15 and lng_tmpcnt <> 17 then
                                    GetField = GetField & lng_tmpcnt & " (" & obj_Recordset2.Fields(FieldNumber).Properties(lng_tmpcnt).name & "(" & obj_Recordset2.Fields(FieldNumber).Properties(lng_tmpcnt).type & ")= " & obj_Recordset1.Fields(FieldNumber).Properties(lng_tmpcnt).value & ")<br>" & vbcrlf
                                end if
                            next 
                        case 3
                            for lng_tmpcnt=0 to obj_Recordset3.Fields(FieldNumber).Properties.count-1
                                if lng_tmpcnt <> 15 and lng_tmpcnt <> 17 then
                                    GetField = GetField & lng_tmpcnt & " (" & obj_Recordset3.Fields(FieldNumber).Properties(lng_tmpcnt).name & "(" & obj_Recordset3.Fields(FieldNumber).Properties(lng_tmpcnt).type & ")= " & obj_Recordset3.Fields(FieldNumber).Properties(lng_tmpcnt).value & ")<br>" & vbcrlf
                                end if
                            next 
                        case else
                    end select
            end select
        end if
    end function

1 个答案:

答案 0 :(得分:2)

此函数采用SP的名称和参数数组,并返回断开连接的记录集

Function RunSPReturnRS(strSP, params())
    On Error Resume next

    ''//Create the ADO objects
    Dim rs , cmd
    Set rs = server.createobject("ADODB.Recordset")
    Set cmd = server.createobject("ADODB.Command")

    ''//Init the ADO objects  & the stored proc parameters
    cmd.ActiveConnection = GetConnectionString()
    cmd.CommandText = strSP
    cmd.CommandType = adCmdStoredProc
    cmd.CommandTimeout = 900 ' 15 minutos

    collectParams cmd, params

    dim i 
    for i = 0 to ubound( Params )
      ''//Use: .CreateParameter("@inVar1", 200, 1, 255, VALUE1) to create the parameters
      cmd.Parameters.Append Params(i)
    next


    ''//Execute the query for readonly
    rs.CursorLocation = adUseClient
    rs.Open cmd, , adOpenForwardOnly, adLockReadOnly

    If err.number > 0 then
        exit function
    end if

    ''//Disconnect the recordset
    Set cmd.ActiveConnection = Nothing
    Set cmd = Nothing
    Set rs.ActiveConnection = Nothing

    ''//Return the resultant recordset
    Set RunSPReturnRS = rs

End Function

或者查看此问题,了解有关创建参数Execute Stored Procedure from Classic ASP

的更多信息