Click to See Complete Forum and Search --> : How to find the no of records in a recordset
October 6th, 1999, 08:23 AM
Hi Everybody,
I have a record set with me. How do I find the no of rows present in that recordset. I tried using GetRecordCount()
function, but this function always returns 1. But on the backend I have multiple records. How to resolve this. Kindly help me.
Thanks in advance,
Regards,
Bala.
Oleg Lobach
October 6th, 1999, 08:36 AM
Hi,
Use "SELECT COUNT (*) From TABLE_NAME" to get count of records:
rst.Open(CRecordset::snapshot,"SELECT COUNT (*) From TABLE_NAME",CRecordset::none);
int nField=0;
long nCount=0;
if(!rst.IsEOF())
{
CString str;
rst.GetFieldValue(nField,str);
nCount=atol(str);
}
rst.Close();
//after that open with required SQL
rst.Open(CRecordset::snapshot,"SELECT Fileld1, Fileld2 From TABLE_NAME",CRecordset::none);
Hope this helps,
Oleg.
October 6th, 1999, 09:05 AM
Thank u Oleg.. U code works fine.. Thanks a lot.
Regards,
Bala
Shahzad
October 6th, 1999, 10:06 AM
In order to use GetRecordCount, you have to necessarily navigate through all records first (I used it working with DAO).
int count;
if(!rst.IsEOF())
{
rst.MoveNext();
}
count = GetRecordCount();
rst.Close();
If you omit the 'if' clause totally, you will get count = 1, because it knows only about the first record at that time (assuming records do exits).
-Shahzad
sqrly
October 8th, 1999, 02:52 PM
This seems a little easier to me and it is how the docs say...
// Set the record count.
CString strCount; // Could use a char*.
while(!m_pSet->IsEOF())
{
m_pSet->MoveNext();
}
ltoa(m_pSet->GetRecordCount(), strCount.GetBuffer(50), 10);
strCount.ReleaseBuffer();
m_editRecordCount.SetWindowText(strCount);
senthil
October 8th, 1999, 03:25 PM
To find out the No. of records in a recordset, you should first use MoveLast or FindLast method of the recordset and then invoke the GetRecordCount Method. No need to traverse all the records in a loop. It is not required. Just open the recordset, call the MoveLast method and then use GetRecordCount to get the total no. of records present in the set.
January 12th, 2000, 09:36 AM
but MoveLast is slow if there r alot of records ..
and count * can not be applied on a Query ... can u help me in that ?
thanx in advance ..
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.