Delphi While循环通过查询中的记录集时出现问题

Delphi While循环通过查询中的记录集时出现问题,delphi,ms-access,delphi-xe2,ms-access-2013,Delphi,Ms Access,Delphi Xe2,Ms Access 2013,我对查询返回的数据的迭代有问题。我使用Delphi XE2和MS Access作为数据库管理系统 我在每次尝试遍历记录集时都会遇到问题,它只会处理第一条记录,然后跳过其余的记录。查询是正确的,因为我使用了RecCount函数,它返回了正确数量的记录,但While循环不会遍历记录,它只会迭代一次,然后继续 以下是部分编码: DB : TADOQuery; //Where DB is this //Only retrieves the OrderIDs belonging to that user

我对查询返回的数据的迭代有问题。我使用Delphi XE2和MS Access作为数据库管理系统

我在每次尝试遍历记录集时都会遇到问题,它只会处理第一条记录,然后跳过其余的记录。查询是正确的,因为我使用了RecCount函数,它返回了正确数量的记录,但While循环不会遍历记录,它只会迭代一次,然后继续

以下是部分编码:

DB : TADOQuery; //Where DB is this

//Only retrieves the OrderIDs belonging to that username as the Object searchs for its own information using the orderID
DB.Close;
DB.SQL.Text := 'SELECT OrderID FROM tblOrders WHERE Username = ' + '''' + pUsername + '''';
DB.ExecSQL;
DB.Open;

ShowMessage(IntToStr(DB.RecordCount));
fCount := 0;
while NOT(db.Eof) AND (fCount < 10) do
Begin
  Inc(fCount);

fArr[fCount] := TOrder.Create(DB.FieldByName('OrderID').AsInteger); //Creating of the object

DB.Next;
end;

DB.Close;
这似乎是可行的,我不知道为什么它不能与第一个循环中的create一起工作。希望还有人能帮忙

谢谢
Ben

请尝试这种模式。它必须按预期工作,否则代码中确实有错误。请注意,DB.ExecSQL在这里毫无意义,一旦要打开查询,就不要执行其他语句,如INSERT、DELETE或UPDATE。 此外,如果您的数据集(DB)没有附加到可视控件上,请考虑在DisableControls和EnabelCon控件中添加循环。当ADO数据集中有许多记录时,这对循环速度有很大影响

DB: TADOQuery;

DB.Close;
DB.SQL.Text := 'SELECT OrderID FROM tblOrders WHERE Username = ' + '''' + pUsername + '''';
DB.Open;
DB.DisableControls;
try
  DB.First;
  while not DB.EOF do
  begin
    // do something here with each record
    DB.Next;
  end;
finally
  DB.EnableControls;
end;

使用
DB.ExecSQL
DB.Open
的原因是什么?您还应该使用参数,并请显示更多代码:
fCount
声明在哪里?
DB
在哪里声明?
TOrder.Create()
上发生了什么?您是否在此构造函数中加载了订单,并为此重用了
DB
fCount
?看起来像是一个副作用,一个简单的测试将向您展示:使用
TOrder.Create()
取消对行的注释,并使用调试程序查看循环,尝试导航到循环之前的第一条记录:DB.first;而…@SirRufo出于某种原因,它是TOrder.Create()。我把它放在一个单独的循环中,现在它工作了。看编辑。谢谢我用了你的模式,我想出了一个解决问题的办法。似乎
code
fArr[fCount]:=TOrder.Create(DB.FieldByName('OrderID').AsInteger)
code
出于某种原因停止循环迭代。我只是将orderID加载到一个临时数组中,然后使用另一个For循环遍历这些值并在OrderArr数组中创建它们。我将把它作为我作品的编辑
var
OrderArr : ARRAY[1..10] of Integer;
k, iIndex : Integer;
begin
opendb('DB.mdb');

//Only retrieves the OrderIDs belonging to that username as the Object searchs for its own information using the orderID
  DB.Close;
  DB.SQL.Text := 'SELECT OrderID FROM tblOrders WHERE Username = ' + '''' + pUsername + '''';
  DB.Open;
  DB.DisableControls;

  fCount := 0;
  try
   DB.First;
   while not DB.EOF do
   begin
    // do something here with each record
    Inc(fCount);

    OrderArr[fCount] := DB.FieldByName('OrderID').AsInteger;
    DB.Next;
   end;
  finally
    DB.EnableControls;
   end;

  for k := 1 to fCount do
  Begin

  fArr[k] := TOrder.Create(OrderArr[k]);

  End;
DB: TADOQuery;

DB.Close;
DB.SQL.Text := 'SELECT OrderID FROM tblOrders WHERE Username = ' + '''' + pUsername + '''';
DB.Open;
DB.DisableControls;
try
  DB.First;
  while not DB.EOF do
  begin
    // do something here with each record
    DB.Next;
  end;
finally
  DB.EnableControls;
end;