Search code examples
delphidelphi-xe2tclientdataset

ClientDataSet to Database Table


I have a ClientDataSet which contains multiple fields.

My question is, does anyone know of a way to copy the fields from the ClientDataSet to a database table?

ClientDataSet is connected to a cxgrid, which displays all fields. All the fields with data I would like to try and copy to the table.

Also just to point out...the ClientDataSet is reading the fields and data from an XML file.

Any help would be great


Solution

  • TClientDataSet is in-memory dataset that can work by itself (as you are doing) or in cooperation with a database session. In order to send all the content of the dataset (all the records) you will need to connect it to a TDatasetProvider component, by using the property TClientDataSet.ProviderName.

    Since TClientDataSet (CDS) was originally designed to work in a disconnected way, in order to support multi-tier applications, the connection between the CDS and its provider is not by reference, as it is with TDataSource referencing a dataset. The ProviderName property is a string that shows the name of the provider.

    Another way is to assign the property TClientDataSet.Provider if they are both in the same DataModule, but this will happen by code and not by the ObjectInspector.

    TDatasetProvider needs a dataset that will receive all the pending rows from the CDS. TDatasetProvider also produces some events that may be used to have a finer grained control over the updates (my preference).

    When a CDS has records that were changed and need to be persisted, your call the TClientDataset.UppyUpdates method, that produces a delta packet (all the records that were inserted, deleted or modified) and send it to the provider, that will handle it in order to persist each of the records.

    If no error occur during the update, the provider sends the CDS another version of the delta and it will be reconciled with the current content of the CDS. As a result, the pending status of the rows are cleared for another round of operations.

    So, in summary:

    1. Add a TDatasetProvider and connect your CDS to it
    2. Add another dataset (one that can work with your data server) and connect the provider to it
    3. Call cds.ApplyUpdates(0) when you want to send the CDS delta to the provider
    4. Read more about TClientDataset, TDatasetProvider and the datasnap in Delphi.