-1

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

Sharpie
  • 373
  • 2
  • 15
  • 34
  • How to do it depends on the database. What database are you intending to use, and which Delphi components to access it? – MartynA Jul 24 '15 at 10:56
  • Sybase ASA 7, using TDataSource to access the cds – Sharpie Jul 24 '15 at 10:59
  • @Dag [edit] your question with more info, it is very unclear. Is that ClientDataSet connected to a query, table, nothing? How are your components linked? What do you want to copy from where to where? There should never be vague terms like *the fields*, *the clientdataset*, or *a database table* in a question. – Jan Doggen Jul 24 '15 at 11:04
  • I have edited it with as much info as I can think of, If im still not clear then please let me know – Sharpie Jul 24 '15 at 11:15
  • Do you need to create the table, or the table already exists? If the table already exists you need a TDatasetProvider and a dataset connected to the table. Than you can connect another TClientDataset to the provider and have it copy data from your TClientDataset and "appy" them to the table. If you need to create the table, you need to create yourself the correct CREATE TABLE statement. – LDS Jul 24 '15 at 11:31
  • @LDS Table already exists, any chance of you providing example of 'copying data from tclientdataset and applying to table' ? – Sharpie Jul 28 '15 at 09:48

1 Answers1

1

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.
AlexSC
  • 1,823
  • 3
  • 28
  • 54