Correct way to write multiple records at once dbgrid and clientdataset

1

I have to write several records at once in the database, I am using the form below, however I think giving applyupdate to every interaction is not the correct way. Is there 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;
    
asked by anonymous 30.01.2015 / 23:45

1 answer

1

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

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

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

So I think this is the most correct way to use this function.

More information:

link

    
17.02.2015 / 23:21