Recover Auto Increment Id from Datasnap Delphi XE3

Asked

Viewed 629 times

2

Using Delphi XE3 / Datasnap with Firedac

When I have the parent table saved on the server I want to recover the value that was added via auto increment in the parent so that I can inform this new value in the foreign key in the daughter table.

I don’t know where to pick up and how to pick up.

...AfterUpdateRecord(Sender: TObject;
  SourceDS: TDataSet; DeltaDS: TCustomClientDataSet; UpdateKind: TUpdateKind);

or

...BeforeUpdateRecord(Sender: TObject;
  SourceDS: TDataSet; DeltaDS: TCustomClientDataSet; UpdateKind: TUpdateKind;
  var Applied: Boolean);

2 answers

1


On the server (Tadquery and Tdatasetprovider), in the Tadquery Master 'Afterpost' event, the Refresh command must be executed and updated with the ID of the autoincrement that has just been executed "I created a Fid variable : Integer to receive the value returned from the database for use in the child dataset"

As an example below:

procedure TsrmNFSe.ADQueryAfterPost(DataSet: TDataSet);
begin
   ADQuery.Refresh;
   FId    := ADQuery.fieldbyname('id_campo_autoincremento').AsInteger;
end;

0

The most indicated way for you to do this is by making event use [BeforeUpdateRecord][1]. In this event you can change the Delta that is being sent to the bank (controlling even the type of change that is happening. You can store the new ID inside a private DM variable (or the main object) and then use it in the items.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.