【问题标题】:Classic ASP For Each Loop terminating too early每个循环的经典 ASP 过早终止
【发布时间】:2013-05-24 09:13:11
【问题描述】:

我的一个 ASP 脚本有一个非常奇怪的问题。它是一个简单的脚本,将信息从数据库读取到记录集中并循环通过记录集,每次都将 HTML 输出为表格行。

我遇到了一个问题,它会周期性地接近每个循环的结束,并且它只是停止而没有通过所有记录集。我知道它正在停止,因为我生成的 HTML 在记录集中只下降到 S 或 T。该脚本没有崩溃,因为在 for each 循环下我结束了表格并且所有 HTML 仍然存在。

奇怪的是,刷新一次就坏了,然后我下次刷新就可以了——SQL 数据库中的数据是静态的,而 ASP 脚本没有改变。一个负载它可以工作,下一个它可以破坏。

我不知道这里发生了什么,所以欢迎任何建议!

【问题讨论】:

  • 你能给我们看看脚本吗?

标签: iis asp-classic


【解决方案1】:

我倾向于做的一件事是将 SQL 写入页面,这样我就可以看到发生了什么。此外,将记录集的计数写入页面以进行调试可能会有所帮助。

尝试使用这样的东西:

const C_NO_DATA             = "NO_DATA"
const C_ERROR                 = "ERROR"
const C_COL_IDENTIFIER        = 0
const C_COL_ERROR_ID        = 1
const C_COL_ERROR_MESSAGE    = 2
const C_COL_SQL                = 3
const C_COL_CONNECTION        = 4

function GetDataSet(sqlString, connString)
    'Initialise...
    dim returnVal, rsData
    on error resume next
        'Define and open the recordset object...
        set rsData = Server.CreateObject("ADODB.RecordSet")
        rsData.Open sqlString, connString, 0, 1, 1
        'Initialise an empty value for the containing array...
        redim returnVal(0,0)
        returnVal(0,0) = C_NO_DATA
        'Deal with any errors...
        if not rsData.EOF and not rsData.BOF then
            'Store the data...
            returnVal = rsData.GetRows()
            'Tidy up...
            rsData.close
            set rsData = nothing
            select case err.number
                case 3021    'No data returned
                    'Do nothing as the initial value will still exist (C_NO_DATA)
                case 0        'No error
                    'Do nothing as data has been returned
                case else
                    redim returnVal(4,0)
                    returnVal(C_COL_IDENTIFIER,0) = C_ERROR
                    returnVal(C_COL_ERROR_ID,0) = err.number
                    returnVal(C_COL_ERROR_MESSAGE,0) = err.description
                    returnVal(C_COL_SQL,0) = sqlString
                    returnVal(C_COL_CONNECTION,0) = connString
            end select
        end if
    on error goto 0
    'Return the array...
    GetDataSet = returnVal
end function

此例程会将数据直接读入一个数组,以便您在闲暇时对其进行检查。

-- 编辑--

补充一点,我提供这样的代码的原因是作为一个函数来一次性提取所有数据,而不是循环通过打开的数据库连接。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-09
    • 2011-11-23
    • 1970-01-01
    相关资源
    最近更新 更多