TClientDataSet used as a dataset in memory - can updates be applied in memory without storing data in a database?

By default, TClientDataSet monitors all changes made to the DataSet (inserts, updates, deletes). Is there a way to tell the dataset to accept the current changes (after a series of inserts using insert / post, say) without actually calling the database to save something?

One of the ideas that I was thinking about is to use the TDataSetProvider and implement the BeforeUpdateRecord event and set the Applied parameter to true. I do not like two things about this. I have to add two more objects (TDataSetProvider and TSQLQuery object), and ApplyUpdates starts the transaction. Is there an easier way?

If I did not set the provider name in TClientDataSet, ApplicationUpdates will fail.

thank

+4
source share
3 answers

You can set it LogChangesto false before modifying the dataset. In addition, if you need a change log at any stage, you can call MergeChangeLogto enable updates.

+5
source

Remember that there is an error (at least as far back as D2006) that can cause you a big headache: if LogChanges is False, some of the filter functions do not work correctly: records may remain inside the filtered data set, even if their values ​​do not match the specified ones filter criteria.

See unit test:

  procedure TestCDS.TestLogChanges;
  var CDS: TClientDataset;
  begin
     CDS := TClientDataset.Create(NIL);
     try
        CDS.FieldDefs.Add('MyStringField', ftString, 20 );
        CDS.CreateDataSet;
        try
           CDS.LogChanges := False;
           CDS.Filter := 'MyStringField is null';
           CDS.Filtered := True;
           Check( CDS.RecordCount= 0);
           CDS.Insert;
           CDS.Post;
           Check( CDS.RecordCount= 1);
           CDS.Edit;
           CDS.FieldByName('MyStringField').AsString := 'Text';
           CDS.Post;
           Check( CDS.RecordCount= 0, 'Recordcount is not 0 after changing value!');
        finally
           CDS.Close;
        end;
     finally
        CDS.Free;
     end;
  end;

, , , : http://www.delphigroups.info/2/f0/463155.html

+1

. , FastReports PDF Excel. . , , , , .

0

Source: https://habr.com/ru/post/1530350/


All Articles