Displaying Source Code(s)
|
|
Count Records
--------------------------------------------------------------------------------
Description : 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:
<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/learn/dbtablegetrow.asp)
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.
|
|
|