AnyDAC
ContentsIndexHome
PreviousUpNext
TADDataSet.CopyDataSet Method

Copies records from source dataset to this dataset.

Group
Links
Parameters 
Description 
ASource: TDataset 
A source dataset. 
AOptions: TADCopyDataSetOptions = [coRestart, coAppend] 
A copy options. The default value is [coRestart, coAppend]. 

Use the CopyDataSet method to copy the records from an ASource dataset to this dataset. 

The method performs steps:

  • copies ASource dataset structure into Self dataset, if coStructure is in AOptions, and:
    • copies ASource dataset indexes setup into Self dataset, if coIndexesCopy is in AOptions. Or resets Self indexes setup, if coIndexesReset is in AOptions;
    • copies ASource dataset aggregates setup into Self dataset, if coAggregatesCopy is in AOptions. Or resets Self aggregates setup, if coAggregatesReset is in AOptions;
    • copies ASource dataset constraints setup into Self dataset, if coConstraintsCopy is in AOptions. Or resets Self constraints setup, if coConstraintsReset is in AOptions;
  • opens Self dataset, if it is inactive;
  • sets a ASource to a beginning, if soRestart is in AOptions;
  • for each record in ASource do:
    • if coEdit is in AOptions, then locate corresponding record in Self dataset, using primary key field values;
    • if coAppend is in AOptions, then append new record to Self dataset;
    • copies all fields, which names exists in both datasets and which are modifiable. If ASource field is not found in this dataset, then it will be skipped. It is important that fields with the same name in both datasets must be compatible by data types. The CopyDataSet method does not verify, that fields are assignment compatible.
    • post the record change and select next record in ASource.

Use coRefresh instead of coEdit to change records only in a local cache. And do not post changes to a database. This option may be used to refresh this dataset using records from ASource dataset. 

This method is similar to assigning to Data property. The differences are:

  • CopyDataSet can copy from non-AnyDAC dataset. Data property allows to copy data only from an AnyDAC dataset.
  • CopyDataSet works through TDataSet API, firing appropriate events. Data property works through DatS API, no events are fired.
  • CopyDataSet copies only current field values. Data property copies all record field versions and preserves row state (inserted, deleted, updated or unchanged).
  • Assigning to Data is much faster, than CopyDataSet.
procedure CopyDataSet(ASource: TDataset; AOptions: TADCopyDataSetOptions = [coRestart, coAppend]);

CopyRecord, Data, TADDataMove

// copies dataset structure and all records
ADMemTable1.CopyDataSet(Query1, [coStructure, coRestart, coAppend]);

 

// refreshed dataset data using records from ADQuery2 dataset. If a record is not found, then it will be appended
ADQuery1.CopyDataSet(ADQuery2, [coRestart, coRefresh, coAppend]);
What do you think about this topic? Send feedback!