经典ASP - 失去记录集?

时间:2012-05-09 00:53:01

标签: stored-procedures asp-classic adodb recordset

早上好,

请耐心等待;我继承了一个经典的ASP环境,这当然是出乎意料的,而不是我的特权。在此先感谢您的考虑。

设置是这样的;存储过程,接受参数以根据传递的参数返回用户活动数据。存储过程在我从SQL Management Studio运行时期望的每个参数情况中返回我的预期结果。 SQL Server环境是SQL Server 2008 Service Pack 3 10.00.5500.00

位于管理面板中的经典ASP页面包含一个包含位置列表的下拉菜单。如果您选择一个位置,页面将重新加载到该位置“实例”;包含相应location_id的全局SITE_ID变量用正确的loaction_id填充。我通过输出SITE_ID进行测试以确保。

问题是在页面加载时它有时会将结果返回到页面,但有时却没有。当它完成时,数据完全符合预期。当没有返回记录集时,我注意到如果我在几分钟后刷新页面,页面通常会正确显示记录集。如果我切换到另一个位置实例,它总是最初没有返回任何内容,但是几分钟后它通常会在刷新时返回。

我不能把我的手指上,并怀疑的代码,我不是很熟悉(但一般可以理解这是怎么回事)可能是关闭连接尽快什么?

有趣的是;我将下面的代码片段添加到页面上的输出中,当记录集返回此代码段时不会触发。当记录集未返回时,页面上将显示所有五条Response.Write消息。

欢迎任何想法或建议。我很感激你的时间。

adState代码段

<%
    If rs.State = adStateClosed Then
        Response.Write "Recordset is closed<br />"
    End If

    If rs.State = adStateConnecting Then
        Response.Write "Recordset is connecting<br />"
    End If

    If rs.State = adStateExecuting Then
        Response.Write "Recordset is executing<br />"
    End If

    If rs.State = adStateFetching Then
        Response.Write "Recordset is fetching records<br />"
    End If

    If rs.State = adStateOpen Then
        Response.Write "Recordset is open<br />"
    End If
%>

以下是存储过程:

存储过程

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [report].[user_recent_activity] (
    @locationID INT,                        -- Location filter, NULL for all if @mode = 'all'
    @mode NVARCHAR(6) = NULL,               -- Mode filter, NULL for location specific
    @userID INT = NULL,                     -- User filter, if passed
    @startDate DATETIME = NULL,
    @endDate DATETIME = NULL
)
AS
SET NOCOUNT ON
SET ANSI_WARNINGS OFF
BEGIN
IF @mode = 'all'
BEGIN   
    SELECT 
        u.[user_id],
        u.[display_name], 
        CONVERT(VARCHAR,u.[last_login],103) + ' ' + CONVERT(VARCHAR,CONVERT(TIME,u.[last_login]),0) [last_login],
        CONVERT(VARCHAR,u.[last_activity],103) + ' ' + CONVERT(VARCHAR,CONVERT(TIME,u.[last_activity]),0) [last_activity],
        l.[location_name],
        (
            SELECT TOP 1 
                [event_text]
            FROM 
                [dbo].[siteflex_events] (NOLOCK) 
            WHERE 
                [location_id] = u.[location_id] AND 
                [user_id] = u.[user_id] AND 
                [event_date] = u.[last_activity]
        ) [activity]
    FROM 
        [dbo].[v_users] u (NOLOCK)
        JOIN [dbo].[locations] l (NOLOCK) ON u.[location_id] = l.[location_id]
    WHERE 
        u.[user_status] = 1 AND 
        u.[last_login] > DATEADD(d,-1,GETDATE())
    ORDER BY
        u.[last_activity] DESC
END
ELSE IF @mode = 'user'
BEGIN
    SELECT 
        MAX([last_login]) [last_login]
    FROM 
        [dbo].[v_users] (NOLOCK)
    WHERE 
        [location_id] = @locationID AND
        [user_id] = @userID 
    SELECT 
        u.[user_id],
        u.[display_name], 
        CONVERT(VARCHAR,u.[last_login],103) + ' ' + CONVERT(VARCHAR,CONVERT(TIME,u.[last_login]),0) [last_login],
        CONVERT(VARCHAR,u.[last_activity],103) + ' ' + CONVERT(VARCHAR,CONVERT(TIME,u.[last_activity]),0) [last_activity],
        l.[location_name],
        e.[event_text] [activity]
    FROM 
        [dbo].[v_users] u (NOLOCK)
        JOIN [dbo].[locations] l (NOLOCK) ON u.[location_id] = l.[location_id]
        JOIN [dbo].[siteflex_events] e (NOLOCK) ON e.[user_id] = u.[user_id] AND e.[location_id] = @locationID
    WHERE 
        u.[user_status] = 1 AND
        u.[location_id] = @locationID AND
        u.[user_id] = @userID
    ORDER BY
        u.[last_activity] DESC      
END 
ELSE IF @mode = 'active'
BEGIN
    SELECT 
        u.[user_id],
        u.[display_name], 
        COUNT(e.[event_id]) [activity_count],
        u.[login_count],
        MAX(CONVERT(VARCHAR,u.[last_activity],103) + ' ' + CONVERT(VARCHAR,CONVERT(TIME,u.[last_activity]),0)) [last_activity],
        l.[location_name]
    FROM 
        [dbo].[v_users] u (NOLOCK)
        JOIN [dbo].[siteflex_events] e (NOLOCK) ON e.[user_id] = u.[user_id] 
        JOIN [dbo].[locations] l (NOLOCK) ON e.[location_id] = l.[location_id]
    WHERE 
        u.[user_status] = 1 AND
        e.[user_id] <> 63 AND
        e.[location_id] = ISNULL(@locationID,e.[location_id]) 
    GROUP BY
        u.[user_id],
        u.[display_name], 
        u.[login_count],
        l.[location_name]       
    ORDER BY
        COUNT(e.[event_id]) DESC                
END 
ELSE IF @mode = 'aging'
BEGIN
    DECLARE @aged TABLE (
        [location_id] INT,
        [location_name] VARCHAR(512),
        [activity_count] INT,
        [last_activity] DATETIME,
        [last_user] INT,
        [activity] VARCHAR(150)
    )
    INSERT INTO @aged
    SELECT  
            l.[location_id],
            l.[location_name],
            COUNT(e.[event_id]) [activity_count],
            (
                SELECT TOP 1
                    [event_date]
                FROM 
                    [dbo].[siteflex_events] (NOLOCK) 
                WHERE 
                    [location_id] = l.[location_id] AND
                    [user_id] <> 63
                ORDER BY
                    [event_date] DESC
            ) [activity],
            (
                SELECT TOP 1
                    [user_id]
                FROM 
                    [dbo].[siteflex_events] (NOLOCK) 
                WHERE 
                    [location_id] = l.[location_id] AND
                    [user_id] <> 63
                ORDER BY
                    [event_date] DESC
            ) [last_user],  
            (
                SELECT TOP 1
                    [event_text]
                FROM 
                    [dbo].[siteflex_events] (NOLOCK) 
                WHERE 
                    [location_id] = l.[location_id] AND
                    [user_id] <> 63
                ORDER BY
                    [event_date] DESC
            ) [activity]                    
        FROM 
            [dbo].[locations] l (NOLOCK)
            JOIN [dbo].[siteflex_events] e (NOLOCK) ON e.[location_id] = l.[location_id]
            JOIN [dbo].[v_users] u (NOLOCK) ON e.[user_id] = u.[user_id]
        WHERE 
            --u.[user_status] = 1 AND
            l.[location_type] <> -1 AND
            e.[user_id] <> 63 AND
            e.[location_id] = ISNULL(@locationID,e.[location_id]) 
        GROUP BY
            l.[location_id],
            l.[location_name]       
    SELECT
        a.[location_id],
        a.[location_name],
        a.[activity_count],
        a.[last_activity],
        a.[last_user],
        u.[display_name],
        a.[activity],
        CONVERT(VARCHAR,DATEDIFF(day, a.[last_activity], GETDATE())) + ' days' [aged]
    FROM    
        @aged a
        JOIN [dbo].[v_users] u (NOLOCK) ON a.[last_user] = u.[user_id]
    ORDER BY
        a.[last_activity]
END
ELSE
BEGIN
    SELECT 
        u.[user_id],
        u.[display_name], 
        CONVERT(VARCHAR,u.[last_login],103) + ' ' + CONVERT(VARCHAR,CONVERT(TIME,u.[last_login]),0) [last_login],
        CONVERT(VARCHAR,u.[last_activity],103) + ' ' + CONVERT(VARCHAR,CONVERT(TIME,u.[last_activity]),0) [last_activity],
        l.[location_name],
        (
            SELECT TOP 1 
                [event_text]
            FROM 
                [dbo].[siteflex_events] (NOLOCK) 
            WHERE 
                [location_id] = @locationID AND 
                [user_id] = u.[user_id] AND 
                [event_date] = u.[last_activity]
        ) [activity]
    FROM 
        [dbo].[v_users] u (NOLOCK)
        JOIN [dbo].[locations] l (NOLOCK) ON u.[location_id] = l.[location_id]
    WHERE 
        u.[user_status] = 1 AND
        u.[location_id] = @locationID
    ORDER BY
        u.[last_activity] DESC      
END 
END

以下是ASP代码。我为这篇文章的篇幅道歉。

ASP页

<!--#include virtual="/admin/inc/setup.asp" -->
<!--#include virtual="/admin/users/common.asp" -->
<%

PAGE_FORM = false
PAGE_STYLE_FILE = "style/user.css"

ADMIN_TABLE = ""

Dim h, g, rs

h = ""

' --- USER PERMISSIONS ---

if not isset(PAGE_ID) then
    if not session("solutions_user") then
        GotoUrl("/admin/users/activity.asp")
    end if
end if

' --- DATE SEARCH ---

min_date = session("min_date")
max_date = session("max_date")

' DATE RANGE SELECTOR SUBMIT
if isset(request.form("btnSubmit")) then
    set f = request.form
    min_date = f("tbMinDate")
    max_date = f("tbMaxDate")
    if (min_date<>"" and max_date<>"") then
            session("min_date")=min_date
            session("max_date")=max_date
        else
            session("min_date")=""
            session("max_date")=""
    end if
end if

' --- MAIN HTML AREA ---

if not isset(PAGE_ID) OR cstr(PAGE_ID)=cstr(ADMIN_ID) OR session("solutions_user") OR (session("user_publishes") = 2 OR session("user_publishes") = 3) then

    ' --- CHECK PERMISSIONS ---

    if session("user_publishes") <> 2 AND session("user_publishes") <> 3 then
        GotoUrl("/admin/users/user.asp")
    end if
    if session("solutions_user") then
        restrictedView = false
    end if

    ' --- CALL STORED PROCEDURES ---

    if pageMode="all" then
        sql = "EXEC report.user_recent_activity " & SITE_ID & ", 'all'"
    elseif pageMode="active" and Request.QueryString("t")="" then
        sql = "EXEC report.user_recent_activity " & SITE_ID & ", 'active'"
    elseif pageMode="active" and Request.QueryString("t") = 1 then
        sql = "EXEC report.user_recent_activity NULL, 'active'"
    elseif pageMode="aging" then
        sql = "EXEC report.user_recent_activity NULL, 'aging'"
    elseif IsSet(PAGE_ID) then
        sql = "EXEC report.user_recent_activity " & SITE_ID & ", 'user', " & PAGE_ID
    else
        sql = "EXEC report.user_recent_activity " & SITE_ID & ", NULL"
    end if

    ' --- MAKE DB CONNECTION ---

    set rs = Server.CreateObject("ADODB.Recordset")
    rs.CursorLocation=3
    rs.CursorType=2
    rs.LockType=1
    rs.open sql, DB_CONN_STRING

    ' --- SHOW LAST LOGIN FOR USER SPECIFIC PAGES ---

    if isset(PAGE_ID) then
        if not rs.eof then
        do while not rs.eof
            g = "Last Logged In: " & rs("last_login")
        rs.movenext
        loop
        end if
        set rs = rs.nextrecordset
    end if

    ' --- NOW SHOW THE RESULTS HEADER ---

    h = ""

    h = h & "<table id='results-table' class='results-table' cellspacing='0' style='padding-top: 20px;'>"

    if pageMode="active" then
        h = h & "<tr class='heading'><td>Most Active</td>"
    elseif pageMode="aging" then
        h = h & "<tr class='heading'><td>Location</td>"
    else
        h = h & "<tr class='heading'><td>Recently Logged In</td>"
    end if

    if pageMode="all" then
        h = h & "<td> Location</td>"
    elseif pageMode="active" then
        h = h & "<td>Location</td>"
        h = h & "<td>Login Count</td>"
        h = h & "<td>Activity Count</td>"
    end if

    if pageMode<>"active" and pageMode<>"aging" then
        h = h & "<td>Last Login</td>"
    end if

    if pageMode="aging" then
        h = h & "<td>Activity Count</td>"
    end if

    h = h & "<td>Last Activity</td>"

    if pageMode<>"all" and pageMode<>"active" and pageMode<>"aging" then
        h = h & "<td style='width: 33%;'>Activity</td>"
    end if

    if pageMode="aging" then
        h = h & "<td>Aged</td>"
    end if

    h = h & "</tr>"

    ' --- NOW LOOP THROUGH THE RESULTS ---

    If not rs.EOF then
    Do While Not rs.EOF

        h = h & "<tr>"
        h = h & "<td>"

        if pageMode="aging" then
            h = h & rs("location_name")
        else
            h = h & rs("display_name")
        end if

        h = h & "</td>"

        if pageMode="all" or pageMode="active" then
            h = h & "<td>" & rs("location_name") & "</td>"
        end if

        if pageMode="active" then
            h = h & "<td>" & rs("login_count") & "</td>"
            h = h & "<td>" & rs("activity_count") & "</td>"
        end if

        if pageMode="aging" then
            h = h & "<td>" & rs("activity_count") & "</td>"
        end if

        if pageMode<>"active" and pageMode<>"aging" then
            h = h & "<td>" & rs("last_login") & "</td>"
        end if

        h = h & "<td>" & rs("last_activity") & "</td>"

        if pageMode<>"all" and pageMode<>"active" and pageMode<>"aging" then
            h = h & "<td>" & rs("activity") & "</td>"
        end if

        if pageMode="aging" then
            h = h & "<td>" & rs("aged") & "</td>"
        end if

        h = h & "</tr>"
    rs.MoveNext
    Loop
    End If

    h = h & "</table>"

end if

    ' ADMIN NAV

    dim navHtml
    listCount = 0
    getUserNav()
    ADMIN_NAV = LeftCol("navTree","Users",navHtml,listCount,colBtn)


    ' SETUP THE ANCHOR JUMPS
    dim anchor_jump,anchors

    if session("solutions_user") then
        arrayPush anchors, Array(0,"Activity - All","activity-msd.asp?m=all")
        arrayPush anchors, Array(1,"Aging - All","activity-msd.asp?m=aging")
        arrayPush anchors, Array(3,"Most Active - All","activity-msd.asp?m=active&t=1")
        arrayPush anchors, Array(4,"Most Active","activity-msd.asp?m=active")
    end if
        arrayPush anchors, Array(2,"Activity","activity-msd.asp")

anchor_jump = makeAnchorJump(anchors,"pageFilter","maintainMode,no-detail")

formTable ADMIN_TABLE, h

%>
<!--#include virtual="/admin/inc/header.asp" -->
<div id="mainContainer" class="orange">
    <div id="mainArea" class="box">
        <h2>
            <%  ' PAGE TITLE

                if pageMode="all" then
                    Response.Write "Recent Activity - All"
                elseif pageMode="active" and Request.QueryString("t") = "" then
                    Response.Write "Most Active - " & SITE_NAME
                elseif pageMode="active" and Request.QueryString("t") = 1 then
                    Response.Write "Most Active - All"
                elseif pageMode="aging" then
                    Response.Write "Aging Activity - All"
                elseif isset(PAGE_ID) then
                    Response.Write "User Activity"
                else
                    Response.Write "Activity - " & SITE_NAME
                end if
            %>
        </h2>
        <%= anchor_jump %>
        <div style='padding-left: 5px;'><%= formTableValueRow(g) %></div>
        <div style='padding-left: 5px;'>
            <%  ' FILTER INFORMATION

                if pageMode="all" then
                    Response.Write "Most recent activity by user in all instances over the past 24 hours."
                elseif pageMode="active" and Request.QueryString("t") = "" then
                    Response.Write "Most active users in the " & SITE_NAME & " instance. Shows login and activity counts."
                elseif pageMode="active" and Request.QueryString("t") = 1 then
                    Response.Write "Most active users in all instances. Shows login and activity counts."
                elseif pageMode="aging" then
                    Response.Write "Aging report showing the instances that haven't been updated in some time."
                elseif isset(PAGE_ID) then
                    Response.Write ""
                else
                    Response.Write "Most recent activity by user in the " & SITE_NAME & " instance."
                end if
            %>
        </div>
        <table cellpadding="0" cellspacing="0" class="formTable">
            <tr>
                <td>Date range to view activity:</td>
                <td class="value"><span class="txt floatLeft" style="margin:0 5px 0 20px;"> From </span> <%=MakeDateSelect("tbMinDate","tbWp3",min_date) %><span class="txt floatLeft" style="margin:0 5px 0 20px;"> To </span><%=MakeDateSelect("tbMaxDate","tbWp3",max_date) %>
                </td>
                <td>
                <div class="formButtons">
                    <ul>
                        <%= MakeButton("submit","btnSubmit","Update Date","") %>
                    </ul>
                </div>
                </td>
            </tr>
        </table>
        <table cellpadding="0" cellspacing="0" class="formTable" style='margin-top: -20px;'>
            <tr>
                <td colspan=3>
                    Debug: This locationID is <%=SITE_ID%>, AdminID <%=ADMIN_ID%><br />
                    <%
                        If rs.State = adStateClosed Then
                            Response.Write "Recordset is closed<br />"
                        End If

                        If rs.State = adStateConnecting Then
                            Response.Write "Recordset is connecting<br />"
                        End If

                        If rs.State = adStateExecuting Then
                            Response.Write "Recordset is executing<br />"
                        End If

                        If rs.State = adStateFetching Then
                            Response.Write "Recordset is fetching records<br />"
                        End If

                        If rs.State = adStateOpen Then
                            Response.Write "Recordset is open<br />"
                        End If
                    %>

                    <%=h%>
                </td>
            </tr>
        </table>
    </div>
    <% = LeftCol("navTree","Users",navHtml,listCount,colBtn) %>
    <div id="blankfooter"></div>
</div>
<!--#include virtual="/admin/inc/footer.asp" -->
<%
On Error Resume Next
If IsObject(rs) then
 rs.close
 Set rs = nothing
End if
If IsObject(DB) then
 DB.close
 Set DB = nothing
End if
%>

0 个答案:

没有答案