CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Guest

    How to find the no of records in a recordset

    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.


  2. #2
    Join Date
    Jul 1999
    Location
    Moscow, Russia
    Posts
    667

    Re: How to find the no of records in a recordset

    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.


  3. #3
    Guest

    Thank U

    Thank u Oleg.. U code works fine.. Thanks a lot.

    Regards,
    Bala


  4. #4
    Join Date
    May 1999
    Posts
    388

    Re: How to find the no of records in a recordset

    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


  5. #5
    Join Date
    Jun 1999
    Posts
    143

    Re: How to find the no of records in a recordset


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






  6. #6
    Join Date
    Oct 1999
    Posts
    63

    Re: How to find the no of records in a recordset

    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.


  7. #7
    Guest

    Re: How to find the no of records in a recordset

    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 ..



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured