2

How can I easily move data from TDataSet to TClientDataSet? I need XML representation of the data in TClientDataSet.XMLData property.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Dmitry
  • 14,306
  • 23
  • 105
  • 189
  • Similar question: http://stackoverflow.com/questions/405065/coverting-a-tmyquery-dataset-to-tclientdataset-in-delphi/405107#405107 – Dmitry Apr 29 '14 at 15:08
  • And this question: http://stackoverflow.com/questions/12150775/how-to-convert-rows-of-a-database-query-to-a-xml-file/12152426#12152426 – Dmitry Apr 29 '14 at 15:12

2 Answers2

6

Drop a TDataSetProvider onto the form or datamodule with the ClientDataSet, set the ClientDataSets property ProviderName to the name of the TDataSetProvider. Set the DataSet property of the TDataSetProvider to the other dataset. Open the ClientDataSet.

Uwe Raabe
  • 45,288
  • 3
  • 82
  • 130
  • Unfortunately it doesn't work. Only fields structure was copied successfully. – Dmitry Apr 29 '14 at 11:22
  • As this is the way it is supposed to work, it might be due to some settings in your setup. You can try to call `ClientDataSet.Last` before getting the XML, just to make sure that all data is actually loaded. – Uwe Raabe Apr 29 '14 at 12:09
  • The first row isn't loaded too. So why the last should be loaded? – Dmitry Apr 29 '14 at 12:12
  • Well, I tried here with the description in my answer and it works perfectly. You should provide more information to track down the problem. – Uwe Raabe Apr 29 '14 at 12:41
  • Uwe, can you show the code? I have used from the second answer. Also I have TDBGrid connected to TDataSet. – Dmitry Apr 29 '14 at 13:30
  • I've marked it as the answer but it still doesn't work for me. – Dmitry Apr 29 '14 at 15:13
  • Not much code to show. Just dropped a TTable, TClientDataSet, TDataSetProvider, TButton and TMemo onto a form, connected as told above and wrote this code into the ButtonClick:`ClientDataSet1.Open; Memo1.Lines.Text := ClientDataSet1.XMLData;` – Uwe Raabe Apr 29 '14 at 17:33
1

This is from the perspective of a TClientDataset derivative.

procedure TMyClientDataset.CopyFromDatasetProvider(Dataset: TDataset);
 var DataSetProvider : TDataSetProvider;
begin
  DataSetProvider := nil;
  try
    Close;
    DataSetProvider := TDataSetProvider.Create(nil);
    DatasetProvider.Options := [poNoReset];
    StoreDefs := False;
    DataSetProvider.DataSet := Dataset;
    SetProvider(DataSetProvider);
    Open;
    First;
    SetProvider(nil);
   finally
    if Assigned(DataSetProvider)
     then DataSetProvider.Free;
   end;

end;
  • Thanks, but why did you use `DatasetProvider.Options := [poNoReset];` and `StoreDefs := False;`? – Dmitry Apr 29 '14 at 10:34
  • This code returns `TMyClientDataset.RecordCount` = 0 with the correct fields structure. – Dmitry Apr 29 '14 at 10:55
  • StoreDefs leaves out Filter Information. See Online Help. I actually got this code I found online. Don't know what to tell you, I use this all the time copy ADODatasets to TClientDatasets. – user3583129 May 02 '14 at 17:41