简体   繁体   English

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

[英]Best way to swap two records in TDataset Delphi?

New to delphi and database programming in general but am curious if there is a better way to swap records in a TDataset? 一般而言,它是delphi和数据库编程的新手,但是否对在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. 目前,我已经实现了一个过程,可以将记录向下移动到数据集中,直到它们达到Eof标记。 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. 我所实现的是一个标准的数组样式交换例程,该例程在保留活动记录的同时尝试保留数据等。

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;

It looks like you're using an in-memory dataset, such as TClientDataset. 看起来您正在使用内存中的数据集,例如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. 只需根据要使用的标准设置索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM