数据库查询怎么会少一条记录

cnbuffer 2012-05-01 05:06:37
连到数据库后查询总是会少一条记录,而且查询出来的数据排序也和数据库里不一样。哪里有问题?

下图为数据库里数据


下图为查询结果,查的是“2”



if (!ADOQuery1->Eof)
{
Edit2->Text=ADOQuery1->FieldByName("date")->AsDateTime;
Edit3->Text=ADOQuery1->FieldByName("time")->AsDateTime;
Edit4->Text=ADOQuery1->FieldByName("NT")->AsString;
Edit5->Text=ADOQuery1->FieldByName("WT")->AsString;
Edit6->Text=ADOQuery1->FieldByName("GT")->AsString;


int q=ADOQuery1->RecordCount;

if(ADOQuery1->FindFirst()) //查找结果集中的第一条记录
{
StringGrid1-> Align=alClient;
StringGrid1->RowCount=q; //设置StringGrid1的总行数
StringGrid1-> ColCount=8;
StringGrid1-> Cells[1][0]= "姓名 ";
StringGrid1-> Cells[2][0]= "日期 ";
StringGrid1-> Cells[3][0]= "时间 ";
StringGrid1-> Cells[4][0]= "G.T. ";
StringGrid1-> Cells[5][0]= "W.T. ";
StringGrid1-> Cells[6][0]= "G.T. ";

int m=0;
while(m<q)
{
if(ADOQuery1->FindNext()) //查找结果集中的下一条记录,是不是这里有问题?
{
StringGrid1-> Cells[1][m+1]= ADOQuery1->FieldByName("peifang")->AsString;
StringGrid1-> Cells[2][m+1]= ADOQuery1->FieldByName("date")->AsDateTime;
StringGrid1-> Cells[3][m+1]= ADOQuery1->FieldByName("time")->AsDateTime;
StringGrid1-> Cells[4][m+1]= ADOQuery1->FieldByName("NT")->AsString;
StringGrid1-> Cells[5][m+1]= ADOQuery1->FieldByName("WT")->AsString;
StringGrid1-> Cells[6][m+1]= ADOQuery1->FieldByName("GT")->AsString;
}
m++;

}
}

...全文
403 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ARIKA 2012-05-03
  • 打赏
  • 举报
回复
StringGrid标题也算一行,所以别用StringGrid的行数做计算,直接用QUERY的

用QUERY的记录数+1来设置StringGrid的行数

用QUERY->Eof来判断是否到记录集末尾,用QUERY->Next来移动记录集
我不懂电脑 2012-05-02
  • 打赏
  • 举报
回复
解决了就好.
cnbuffer 2012-05-01
  • 打赏
  • 举报
回复
已解决,谢谢!



[Quote=引用 6 楼 的回复:]

StringGrid1-> ColCount=8;
StringGrid1-> Cells[1][0]= "姓名 ";
StringGrid1-> Cells[2][0]= "日期 ";
StringGrid1-> Cells[3][0]= "时间 ";
StringGrid1-> Cells[4][0]= "G.T. ";
String……
[/Quote]
dataxdata 2012-05-01
  • 打赏
  • 举报
回复
StringGrid1-> ColCount=8;
StringGrid1-> Cells[1][0]= "姓名 ";
StringGrid1-> Cells[2][0]= "日期 ";
StringGrid1-> Cells[3][0]= "时间 ";
StringGrid1-> Cells[4][0]= "G.T. ";
StringGrid1-> Cells[5][0]= "W.T. ";
StringGrid1-> Cells[6][0]= "G.T. ";

int q=ADOQuery1->RecordCount;
StringGrid1->RowCount=q + 1; //设置StringGrid1的总行数
ADOQuery1->First();
for ( int i=0, row=1; i<q; i++, row++ ) {
StringGrid1-> Cells[1][row]= ADOQuery1->FieldByName("peifang")->AsString;
StringGrid1-> Cells[2][row]= ADOQuery1->FieldByName("date")->AsDateTime;
StringGrid1-> Cells[3][row]= ADOQuery1->FieldByName("time")->AsDateTime;
StringGrid1-> Cells[4][row]= ADOQuery1->FieldByName("NT")->AsString;
StringGrid1-> Cells[5][row]= ADOQuery1->FieldByName("WT")->AsString;
StringGrid1-> Cells[6][row]= ADOQuery1->FieldByName("GT")->AsString;
ADOQuery1->Next();
}

dataxdata 2012-05-01
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

查询数据库里的数据时不是按记录顺序查的吗?还用再排序啊?





引用 2 楼 的回复:

可以在SQL语句中加上排序,以明确记录的排列顺序
[/Quote]
不一定,要确保顺序,就要加上排序
cnbuffer 2012-05-01
  • 打赏
  • 举报
回复
查询数据库里的数据时不是按记录顺序查的吗?还用再排序啊?





[Quote=引用 2 楼 的回复:]

可以在SQL语句中加上排序,以明确记录的排列顺序
[/Quote]
cnbuffer 2012-05-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
不对,我设置总行数等于记录总数加1 ,这行加到表格末尾了,见下图。应该是数据查询有问题




int q=ADOQuery1->RecordCount;
StringGrid1->RowCount=q;
StringGrid1-> Cells[1][0]= "姓名 ";
...总行数等于记录总数,然后你标题占了一行阿。那记录肯定就少了1行啊。。。
[/Quote]
dataxdata 2012-05-01
  • 打赏
  • 举报
回复
可以在SQL语句中加上排序,以明确记录的排列顺序
dfdscx 2012-05-01
  • 打赏
  • 举报
回复
int q=ADOQuery1->RecordCount;
StringGrid1->RowCount=q;
StringGrid1-> Cells[1][0]= "姓名 ";
...总行数等于记录总数,然后你标题占了一行阿。那记录肯定就少了1行啊。。。

1,178

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 数据库及相关技术
社区管理员
  • 数据库及相关技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧