在TDataset Delphi中交换两个记录的最佳方法是什么?

时间:2021-02-18 14:05:16

New to delphi and database programming in general but am curious if there is a better way to swap records in a TDataset? I have read through some help and cant find any obvious methods. Currently I have a procedure implemented to move records down the dataset until they hit the Eof marker. However I am getting some odd errors when I get to the last record in my data. All I have is implemented a standard array-style swap routine trying to preserve data and whatnot while juggling active records.

对delphi和数据库编程很陌生,但我想知道是否有更好的方法来交换TDataset中的记录?我看了一些帮助资料,找不到任何明显的方法。目前,我实现了一个过程,将记录从数据集中移动到Eof标记。但是,当我得到数据中的最后一条记录时,我得到了一些奇怪的错误。我只实现了一个标准的arraystyle交换例程,试图在处理活动记录时保存数据。

Code So Far

procedure TForm2.btnDownClick(Sender: TObject);
var
   sTmp,sTmp2  : string;
   iTmp,iTmp2  : integer;
begin
   tblMatched.DisableControls;
   if ( tblMatched.Eof <> true ) then
   begin
      // Grab data to swap
      tblMatched.GotoBookmark( tblMatched.GetBookmark );
      iTmp := tblMatched.Fields[0].AsInteger;
      sTmp := tblMatched.Fields[1].AsString;
      tblMatched.Next;
      iTmp2 := tblMatched.Fields[0].AsInteger;
      sTmp2 := tblMatched.Fields[1].AsString;

      // Swap data
      tblMatched.Prior;
      tblMatched.Edit;
      tblMatched.Fields[0].Value := iTmp2;
      tblMatched.Fields[1].Value := sTmp2;

      tblMatched.Next;
      tblMatched.Edit;
      tblMatched.Fields[0].AsInteger := iTmp;
      tblMatched.Fields[1].AsString := sTmp;
   end;
   tblMatched.EnableControls;
end;

1 个解决方案

#1


3  

It looks like you're using an in-memory dataset, such as TClientDataset. If you simply put an index on the dataset, it will keep things ordered for you so you don't have to rearrange them manually. Just set up the index based on whatever criteria you want to have it use.

看起来您使用的是内存中的数据集,比如TClientDataset。如果您只是在数据集上放置一个索引,它将为您保持事物的有序,因此您不必手动重新排列它们。根据你想要使用的标准来建立索引。

#1


3  

It looks like you're using an in-memory dataset, such as TClientDataset. If you simply put an index on the dataset, it will keep things ordered for you so you don't have to rearrange them manually. Just set up the index based on whatever criteria you want to have it use.

看起来您使用的是内存中的数据集,比如TClientDataset。如果您只是在数据集上放置一个索引,它将为您保持事物的有序,因此您不必手动重新排列它们。根据你想要使用的标准来建立索引。