CDatabase CRecordset - How to get the number of rows?



Hi there,

I'm using Visual C++6 and MFC to connect to a database. So far I have just
been able to connect to the database. I want to count the number of rows in
the database so I can see if there is any data in it. All I want to do is get
the number of rows if possible. I dont need to pass any of the actual data
(if there is any). I've been using the CDatabase class to connect to the
Database and have tried a few things using the CRecordset class to try and
return the number of rows. Here is a cut down version of my code in which I
have removed all the try catch blocks for clarity -

CDatabase db;
db.Open(connectionString);

CRecordset rs(&db);
rs.Open(CRecordset::forwardOnly,_T( "SELECT COUNT (*) FROM Records" ) );

long recordCount = 0;
recordCount = rs.GetRecordCount();

However, when I check the value of recordCount it always returns 1 even when
there are none or many rows in the table.

Any tips on how to do this is much appreciated!

Thanks,

David


.



Relevant Pages

  • Re: counting records in a recordset
    ... Recordcount will work if you specify the proper type of cursor (the default ... Since this is a SQL database (MS SQL Server, ... after it is entered using an expensive client-side cursor. ...
    (microsoft.public.inetserver.asp.general)
  • Re: counting records in a recordset
    ... Well this is how it works: the data is entered into a database somewhere, ... > client-side cursor, recordcount will work (do you have the ADO ... >>> Please reply to the newsgroup. ...
    (microsoft.public.inetserver.asp.general)
  • Re: CDatabase CRecordset - How to get the number of rows?
    ... > I'm using Visual C++6 and MFC to connect to a database. ... I've been using the CDatabase class to connect to the ... > Database and have tried a few things using the CRecordset class to try and ... > recordCount = rs.GetRecordCount; ...
    (microsoft.public.vc.mfc)
  • Re: add data from a datagrid
    ... what can I do, I use on a form a datagrid, and a commandbutton. ... .CursorLocation = adUseServer ... Otherwise the RecordCount property may not be correct. ... This would let the database server count up the records, ...
    (microsoft.public.vb.general.discussion)
  • to find the value of a record using recordcount
    ... is that i m taking the recordcount of both the database. ... if recordcount of one database is 30 and of second is 13. ... then there is 17 data sort in other database. ... but its showing the record cannot be updated as it requires a current ...
    (microsoft.public.vb.syntax)