【问题标题】:how to count records in ASP classic?如何计算 ASP 经典中的记录?
【发布时间】:2016-05-06 11:13:02
【问题描述】:

我对 ASP 经典编程不是很熟悉。我只需要一个小代码就可以在我的网页上运行。返回查询的记录如何统计?

<%
Set rsscroll = Server.CreateObject("ADODB.Recordset")
Dim strSQLscroll, rsscroll
strSQLscroll = "SELECT * FROM tblItems where expiration_date > getdate() order by expiration_date desc;"
rsscroll.open strSQLscroll,oConn
%>

谢谢,

【问题讨论】:

    标签: asp-classic


    【解决方案1】:

    可以(但不推荐)在 Recordset 对象上使用 RecordCount 属性,如下所示:

    iTotalRecords = rsscroll.RecordCount
    

    如果您的表非常大,这可能需要很长时间才能运行。我会改为运行单独的 SQL 查询来获取总记录

    SQL = "SELECT COUNT(*) AS TotalRecords FROM tblItems WHERE expiration_date > getdate() "
    set rsRecordCount = conn.Execute(SQL)
    if not rsRecordCount.Eof then
      iTotalRecords = rsRecordCount.Fields("TotalRecords")
    else
      iTotalRecords = 0
    end if
    rsRecordCount.Close
    set rsRecordCount = nothing
    

    【讨论】:

    • 很棒的方法,对我有用:)
    【解决方案2】:

    rsscroll.RecordCount

    【讨论】:

    • 这可能有效,但它也可能返回 -1。例如,在调用者使用完所有记录之前,默认的 SQL Server Firehose 行集不会产生行计数。
    • 请记住,您需要将 nocount 设置为 on。见stackoverflow.com/a/16617637/356544
    【解决方案3】:

    使用 SQL COUNT 方法的一个简单解决方案。这假设您需要行数而不是数据本身。

    <%
        Set rsscroll = Server.CreateObject("ADODB.Recordset")
        Dim strSQLscroll, rsscroll, intRow
        strSQLscroll = "SELECT COUNT(*) AS Total FROM tblItems WHERE expiration_date > getdate();"
        rsscroll.open strSQLscroll, oConn
        response.write rsscroll("Total")
        rsscroll.close: set rsscroll = nothing 
        oConn.close: set oConn = nothing 
    %>
    

    这会返回一行,其中包含一个名为“Total”的值。 (如果您同时需要行数 数据,请继续阅读。)

    您的查询代码使用默认的 RecordSet,它以“仅转发”模式返回数据以提高效率。它将逐行执行,但不知道实际计数。 (此模式还将 RecordSet.RecordCount 设置为 -1,因此该字段对您没有用处。)

    RecordSet.Open 的“Cursor Type”参数允许您更改为“Keyset”模式(参数值 1),确实将 RecordCount 字段设置为数据行数。 (为了完整起见,“锁定类型”和“命令类型”参数包括在内,但它们并未包含在此答案中。)

    RecordsetObject.Open "TableName|SQLStatement", ConnectionObject [,Cursor Type] [,Lock Type] [,Command Type] 
    

    将此参数添加到代码的 RecordSet.Open 调用中,然后检查 RecordCount。

    <%
        Set rsscroll = Server.CreateObject("ADODB.Recordset")
        Dim strSQLscroll, rsscroll, intRow
        strSQLscroll = "SELECT * FROM tblItems where expiration_date > getdate() order by expiration_date desc;"
        rsscroll.open strSQLscroll, oConn, 1
        intRow = rsscroll.RecordCount
        ' ... do something with intRow
        rsscroll.close: set rsscroll = nothing 
        oConn.close: set oConn = nothing 
    %>
    

    如果数据库性能对您的情况有任何影响,那么 RecordSet.GetRows() 方法的效率要高得多。

    <% 
        Dim rsscroll, intRow, rsArray
        Set oConn = CreateObject("ADODB.Connection") 
        oConn.open "<connection string>" 
        strSQLscroll = "SELECT * FROM tblItems where expiration_date > getdate() order by expiration_date desc" 
        Set rsscroll = conn.execute(strSQLscroll) 
        if not rsscroll.eof then 
            rsArray = rsscroll.GetRows() 
            intRow = UBound(rsArray, 2) + 1 
            response.write "rows returned: " & intRow
            ' ... do any other operations here ... 
        end if 
        rsscroll.close: set rsscroll = nothing 
        oConn.close: set oConn = nothing 
    %>
    

    【讨论】:

      【解决方案4】:

      我通常使用单独的查询,例如“从表中选择计数(*)”来获取计数,因为我通常不仅需要计数,还需要单位数或平均价格或其他内容的总和,并且更容易编写单独查询而不是创建更多变量并在循环内说“TotalUnits = TotalUnits + rs("Units").value”以显示结果。当您需要在结果上方显示总计并且不想循环两次记录集时,它也会派上用场。

      【讨论】:

        【解决方案5】:

        养成将返回的数据存储在数组中的习惯。这比使用开放记录集的迭代速度快得惊人。此外,在执行此操作时指定要选择的字段,因为您必须显式引用数组索引。

        <%
        Set rsscroll = Server.CreateObject("ADODB.Recordset")
        Dim strSQLscroll, rsscroll
        Dim arrCommon
        
        'Open recordset, copy data to array
        strSQLscroll = "SELECT field1, field2, field3 FROM tblItems where expiration_date > getdate() order by expiration_date desc;"
        rsscroll.open strSQLscroll,oConn
            arrCommon = rsscroll.getRows()
        rsscroll.close
        
        'Get the total records in this array
        response.write ubound(arrCommon, 2);
        
        'Loop...
        for i = 0 to ubound(arrCommon, 2)
        
            ' This prints field 3
            response.write arrCommon(2, i)
        
        next
        %>
        

        【讨论】:

          【解决方案6】:

          你可以改变你的 SQL 来计算记录数:

          strSQLscroll = "SELECT count(*) as Total FROM tblItems where expiration_date > getdate();"
          

          那你只需要response.write rsscroll("Total")

          【讨论】:

            【解决方案7】:

            Set rsscroll = Server.CreateObject("ADODB.Recordset") Dim strSQLscroll, rsscroll strSQLscroll = "SELECT *,(SELECT TableID FROM tblItems where expiration_date > getdate()) As Count FROM tblItems where expiration_date > getdate() order by expiration_date desc;" 
            rsscroll.open strSQLscroll,oConn
            Count = rsscroll("Count") 
            

            %>

            【讨论】:

              【解决方案8】:

              你可以试试这个

                  Dim count
                  count = 0
                  if strSQLscroll.eof <> true or strSQLscroll.bof <> true then
                     while not strSQLscroll.eof
                        count = count+1
                        strSQLscroll.movenext
                     wend
                  end if
                  response.write(count)
              

              【讨论】:

                【解决方案9】:

                如果你使用 MySQL,试试这个:

                Dim strSQLscroll, rsscroll, countrs
                
                Set rsscroll = Server.CreateObject("ADODB.Recordset")
                rsscroll.CursorLocation = 3
                rsscroll.open "SELECT * FROM tblItems where expiration_date > getdate()
                order by expiration_date desc;",oConn
                
                countrs = rsscroll.recordcount
                

                【讨论】:

                • 欢迎来到 Stack Overflow!虽然此代码可能会回答问题,但提供有关 为什么 和/或 如何 此代码回答问题的附加上下文会提高其长期价值。
                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2020-10-24
                • 1970-01-01
                相关资源
                最近更新 更多