Mega Code Archive

 
Categories / Delphi / ADO Database
 

Cloning a ClientDataset

Title: Cloning a ClientDataset Question: Cloning ClientDatasets may be usefull when using temporary dataset for simulation purposes. Answer: The first procedure clones a clientdataset field defs and the second copies all records from one to another: ------------------------------------ //FromDataset: the source dataset //ToDataset: the target dataset ------------------------------------ procedure CloneClientDataset(FromClientDataset, ToClientDataset: TCustomClientDataset); var i: integer; begin ToClientDataset.Close; ToClientDataset.Fields.Clear; ToClientDataset.FieldDefs.Clear; for i:= 0 to FromClientDataset.FieldDefs.Count - 1 do if FromClientDataset.FieldDefs[i].DataType ftDataSet then begin with ToClientDataset.FieldDefs.AddFieldDef do Assign(FromClientDataset.FieldDefs[i]); end; ToClientDataset.CreateDataSet; end; ------------------------------------ //FromDataset: the source dataset //ToDataset: the target dataset //FieldNames: a array os strings representing the names of the fields to have //their values copied. ------------------------------------ procedure CloneFieldsValues(FromDataset, ToDataset: TClientDataset; FieldNames: array of string); var FromField, ToField: TField; i: integer; begin for i:= low(FieldNames) to High(FieldNames) do begin FromField:= FromDataset.FindField(FieldNames[i]); ToField:= ToDataset.FindField(FieldNames[i]); if assigned(FromField) and assigned(ToField) then ToField.Value:= FromField.Value; end; end;