how to count records in ASP classic?

前端 未结 9 997
陌清茗
陌清茗 2021-01-14 02:50

I\'m not quite familiar with programming ASP classic. I just need a small code to run on my webpage. How do i count the record of the returned query?

<%
S         


        
相关标签:
9条回答
  • 2021-01-14 02:56

    If you are using MySQL try this:

    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
    
    0 讨论(0)
  • 2021-01-14 03:01

    rsscroll.RecordCount

    0 讨论(0)
  • 2021-01-14 03:06

    It is possible (but not recommended) to use the RecordCount property on the Recordset object as follows:

    iTotalRecords = rsscroll.RecordCount
    

    If your table is really large, this can take a long time to run. I would instead run a separate SQL query to get the total records

    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
    
    0 讨论(0)
  • 2021-01-14 03:06

    I usually use a separate query like "select count(*) from table " to get counts because I usually need not only the count but a summation of the number of units or the average price or whatever and it's easier to write a separate query than to make more variables and say "TotalUnits = TotalUnits + rs("Units").value" inside the loop to display the results. It also comes in handy for the times you need to show the totals above the results and you don't want to loop though the recordset twice.

    0 讨论(0)
  • 2021-01-14 03:13

    <% ' TableID = your tables ID...

    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") 
    

    %>

    0 讨论(0)
  • 2021-01-14 03:15

    You could just change your SQL to count the records:

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

    Then you just need to response.write rsscroll("Total")

    0 讨论(0)
提交回复
热议问题