<%@ LANGUAGE = PerlScript%> ADO Record Counting
ActiveState PerlScript


ActiveX Data Objects (ADO) Record Counting

Counting the number of records in a Recordset is something many find troublesome at first. The solution to why it sometimes return -1 instead of an accurate count is very simple. In order to avoid -1, you must use a cursor that can move all the way to the end of the Recordset and then back. Unless specified, the cursor will default to a cursor that moves only forward in the Recordset, thus can not determine the count. In this example, we use a Keyset cursor, which is a lightweight cursor that can move both forward and backward. When the database connection is open, the Recordset will contain the number of available records matching the query in its RecordCount-property.

<% my $adOpenKeySet_CursorType = 1; $rst = $Server->CreateObject('ADODB.Recordset'); $rst->Open('SELECT * FROM Orders', 'ADOSAMPLES', $adOpenKeySet_CursorType); $Response->Write("There are ".$rst->{RecordCount}." records in the Recordset"); $rst->Close(); # Close the recordset undef($rst); # Destroy the object %>


<% $url = $Request->ServerVariables('PATH_INFO')->item; $_ = $Request->ServerVariables('PATH_TRANSLATED')->item; s/[\/\\](\w*\.asp\Z)//m; $params = 'filename='."$1".'&URL='."$url"; $params =~ s#([^a-zA-Z0-9&_.:%/-\\]{1})#uc '%' . unpack('H2', $1)#eg; %> Return

view the source