gpt4 book ai didi

database - 如何检索表中的行数

转载 作者:搜寻专家 更新时间:2023-10-30 20:06:15 25 4
gpt4 key购买 nike

我正在尝试检索表中的行数,但无论结果是多少,我总是得到 1。

代码如下:

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 x=set.GetRecordCount();
CString temp;
temp.Format("%d",x);
AfxMessageBox(temp);
;

最佳答案

你读过 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.

您没有在各行之间移动。

现在,如果您真的尝试以这种方式计算我的一张表中的行数,我会追上您并用锋利的棍子戳您的眼睛。相反,我通常希望您像这样使用 SQL:

select count(*) num_rows from allpacks;

该 SQL 语句将始终返回一行,其中包含一个名为“num_rows”的列。

关于database - 如何检索表中的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6149523/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com