Search Search

#1 worldwide
FREE Coding Lessons

since 1996
   THE BEST WAY to learn ASP & Asp.net!
Advertise Here!
click for details
Credits Host:
DiscountASP.net
Server Admin:
The "Team"
Contact Info.
Charles M. Carroll

my Blog
[prev. Lesson]  ADO: Limiting Number of Records
     [next Lesson]  ADO: Cursor Types by Phil Paxton

Count Records by Juan Llibre, Charles Carroll

This script counts the records in a database. Many people who attempt to use the .recordcount property have the value -1 returned to them. In a nutshell, -1 means "I don't know how many records this query/table contains". It happens since the default cursor type is AdOpenforwardonly (see below for explanation of all cursor types). Here is the script that counts the query results and WILL NOT return -1:

   filename=/learn/test/dbcount.asp

<Test Script Below>


<head><html>
<TITLE>dbcount.asp</TITLE>
</head><body bgcolor="#FFFFFF">
<!--#INCLUDE VIRTUAL="/ADOVBS.INC" -->
<% 
' change these for your site
connectme="DSN=Student;uid=student;pwd=magic"
sqltemp="select * from publishers where state='NY'"
set rstemp=Server.CreateObject("adodb.Recordset")
rstemp.open sqltemp, connectme, adopenstatic
howmanyrecs=rstemp.recordcount
response.write howmanyrecs & " records in<br>" & sqltemp
rstemp.close
set rstemp=nothing
%>
</body></html>

The critical part here is having a cursor type that supports it (Adopenforward only is the default cursor type and it does NOT support counting records) and including adovbs.inc to define the cursor types. The file adovbs.inc can be obtained from http://www.learnasp.com/adovbs.inc

If you open the database and are able to obtain an accurate record count is by nature a slower connection; it is a burden on a recordset that it must know exact record counts upon demand. The additional overhead and limitations of a adopendynamic or adopenstatic cursor may be a glacially slow way to retrieve the data even though they count accurately. If you must count AND retrieve, you can't go wrong using GetRows (see http://www.learnasp.com/freebook/asp/dbtablegetrow.aspx) because:

  • GetRows can count with any cursor type and is very fast at retrieving and counting!
  • is much faster and conserves server resources, see:
    /advice/whygetrows.asp 

Here is a description of the significance of each cursor type:

Adopenstatic cursor
   The data is dead. If you retrieve a million records for example at 9:00am and it takes 6 minutes to read the data, at the 6th minute you will not be retrieving fresh or recently added data. It is like a snapshot of the data. Recordsets opened this way WILL contain an accurate recordcount.

Adopenforwardonly cursor (the default)
   The data is alive but you can only move forward. Attempts to move backward or to specific record numbers will fail.. Recordsets opened this way WILL NOT contain an accurate recordcount, instead returning -1.

Adopenkeyset cursor
  
The data is alive and any record read will be the most recent data.  f you retrieve a million records for example at 9:00am and it takes 6 minutes to read the data, at the 6th minute you will still be retrieving fresh data but NOT data added or deleted since 9am. Recordsets opened this way WILL NOT contain an accurate recordcount, instead returning -1.

Adopendynamic cursor
   The data is alive and additions will be noticed. If you retrieve a million records for example at 9:00am and it takes 6 minutes to read the data, at the 6th minute you will still be retrieving fresh data and records added to the end of the data.  Recordsets opened this way WILL contain an accurate recordcount.

There are many worthy charities!!. But perhaps help starving children in Africa or South America AND help Charles too. a $5 tip buys him lunch at McDonalds, a $20 tip buys his kid Hitoshi a new computer game, a $39 tip buys his daughter Michiko a few nice outfits. See our donor list.