If the wish is simply to clone objects Delphi supports this through the Tpersistent.Assign method which is the ancestor of the Tcomponent class, which serves as the basis for the visual and non-visual components of Delphi.
This method takes the contents of an object (passed by parameter) to the new one, and would look something like this
procedure ClonazTable(zTableOriginal: TDataset);
begin
zTableClonada.Assign(zTableOriginal);
end;
Another option is to clone objects through Rtti, traversing the object and copying the data from one to the other. There is a good article on the subject in Club Delphi magazine number 113.
However as this component is a query to the database, the information that is brought from the database will not be cloned together. To do this you need to copy the entire dataset structure to something that exists exclusively in memory, such as a Tclientdataset.
To do this, it is necessary to copy the structure first and then the data from the original Dataset to the Dataset in Memory, two of the many options that exist to facilitate this work are:
1. Tjvmemorydata: A Component of the Jedi package (which is free), it has a method that copies the structure of a Dataset to the component, leaving only the work of copying the data.
function CopiarDataSet(zTableOriginal: TDataset): TJvMemoryData;
begin
Result := TJvMemoryData.Create(Application);
**Result.CopyStructure(zTableOriginal);**
zTableOriginal.First;
while not(zTableOriginal.Eof) do
begin
***//Código que copia os dados***
zTableOriginal.Next;
end;
end;
2. Tdxmemdata: A Component of the Developer Express package (which is paid for), it has a method that completely copies the Dataset to a new one.
function CopiarDataSet(zTableOriginal: TDataset): TdxMemData;
begin
Result := TdxMemData.Create(Application);
**Result.CopyFromDataSet(zTableOriginal);**
end;
If it doesn’t work, use Tclientdataset methods
– Edgar Muniz Berlinck
I get it... case to
procedure
Assign
was not programmed, I could create aHelper
for myTZtable
, following the same logic of the methodAssign
?– Thiago Thaison
Yes. It all depends on what properties you will use. For example, to take the data, copy the SQL property and then give an Open would be enough; but you will probably want to take Displaylabel’s of the fields, home formatting, etc... Then just make a copy of everything you need. Bottom line is, overall, everything you’ll need will be focused on the Fields. Make a loop with them passing everything from one field to another would solve.
– RaphaelZ