且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何删除,更新等由Delphi ADO中的查询生成的表?

更新时间:2023-12-05 23:42:16

James L和Hendra都提供了与所选记录相同的NoteID和NoteText的记录。本质如何做你想要的。下面是实现它的一种方法。

Both James L and Hendra provide the essence of how to do what you want. The following is a way to implement it.

procedure TForm1.ADOQuery1BeforeDelete(DataSet: TDataSet);
var
  SQL : string;
begin
  SQL := 'DELETE FROM [Note] WHERE NoteID='+
    DataSet.FieldByName('NoteID').AsString;
  ADOConnection1.Execute(SQL);
  TADOQuery(DataSet).ReQuery;
  Abort;
end;

这将允许TADOQuery.Delete正常工作。中止是必要的,以防止TADOQuery也尝试删除记录后,删除它。主要缺点是TADOQuery.ReQuery不保留光标位置,即当前记录将是第一条记录。

This will allow TADOQuery.Delete to work properly. The Abort is necessary to prevent TADOQuery from also trying to delete the record after you have deleted it. The primary down side is that the TADOQuery.ReQuery does not preserve the cursor position, i.e. the current record will be the first record.

更新:

以下尝试恢复游标。我不喜欢第二个Requery,但是在尝试恢复无效书签(由于删除最后一条记录)后似乎有必要恢复DataSet。这对我的有限测试有效。

The following attempts to restore the cursor. I do not like the second Requery, but it appears to be necessary to restore the DataSet after attempting to restore a invalid bookmark (due to deleting the last record). This worked with my limited testing.

procedure TForm1.ADOQuery1BeforeDelete(DataSet: TDataSet);
var
  SQL : string;
  bm : TBookmarkStr;
begin
  SQL := 'DELETE FROM [Note] WHERE NoteID='+
    DataSet.FieldByName('NoteID').AsString;
  bm := Dataset.BookMark;
  ADOConnection1.Execute(SQL);
  TADOQuery(DataSet).ReQuery;
  try
    Dataset.BookMark := bm;
  except
    TADOQuery(DataSet).Requery;
    DataSet.Last;
  end;
  Abort;
end;