Correct way to record multiple records at once dbgrid and clientdataset

Asked

Viewed 1,306 times

1

I have to record several records at once in the database, I’m using it in the form below, however I think giving applyupdate to each interaction is not the right way. There is another way?

procedure TFrm.Button1Click(Sender: TObject);
Var
i: Integer;
begin
  (ds.DataSet as TClientdataset).Open;
  For i := 1 to 10 do
  begin
    (ds.DataSet as TClientdataset).Insert;
    ds.DataSet.FieldByName('med1').AsInteger:= 1;
    ds.DataSet.FieldByName('med2').AsInteger:= 1;
    ds.DataSet.FieldByName('med3').AsInteger:= 2;
    ds.DataSet.FieldByName('med4').AsDateTime:= Now;
    ds.DataSet.FieldByName('med5').AsFloat:= 1;
    ds.DataSet.FieldByName('med6').AsDateTime:= Now;
    (ds.DataSet as TClientdataset).Post;
    (ds.DataSet as TClientdataset).ApplyUpdates(0);
  end;

end;

1 answer

1


First remember that the ApplyUpdates only works when you are working with CachedUpdates (marked as true in connection properties).

I believe the best way to use the ApplyUpdates is this, and treating possible problems that may occur through the return that the variable itself returns to you.

According to the documentation of the Embarcadero, the return of the function is of type Integer indicating the amount of errors that occurred, and so you may be able to identify when an error occurred and try to have the least possible loss of information that will be sent to the database.

Therefore, I believe that this is the most correct way to use this function.

More information:

http://docwiki.embarcadero.com/Libraries/XE7/en/Datasnap.DBClient.TCustomClientDataSet.ApplyUpdates

Browser other questions tagged

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