开发者

How to retrieve the number of rows in a table

I am trying to retrieve the number of rows in a table but no matter the number i always get 1 as the result.

Here is the code:

UpdateData(TRUE);
CDatabase database;
CString connectionstring, sqlquery, Slno,size,portno,header,id;
connectionstring=TEXT("Driver={SQL NATIVE CLIENT};SERVER=CYBERTRON\\SQLEXPRESS;Database=packets;Trusted_Connection=Yes" );
database.Open(NULL, FALSE, FALSE, connectionstring);
CRecordset set(&database);
sqlquery.Format(TEXT("select * from allpacks;"));
set.Open(CRecordset::forwardOnly, sqlquery, NULL);
int 开发者_JS百科x=set.GetRecordCount();
CString temp;
temp.Format("%d",x);
AfxMessageBox(temp);
;


Did you read the documentation for GetRecordCount()?

The record count is maintained as a "high water mark," the highest-numbered record yet seen as the user moves through the records. The total number of records is only known after the user has moved beyond the last record. For performance reasons, the count is not updated when you call MoveLast. To count the records yourself, call MoveNext repeatedly until IsEOF returns nonzero. Adding a record via CRecordset:AddNew and Update increases the count; deleting a record via CRecordset::Delete decreases the count.

You're not moving through the rows.

Now, if you actually tried to count rows in one of my tables that way, I'd hunt you down and poke you in the eye with a sharp stick. Instead, I'd usually expect you to use SQL like this:

select count(*) num_rows from allpacks;

That SQL statement will always return one row, having a single column named "num_rows".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜