Jump to content
dummzeuch

What does TDataSet.CopyFields do?

Recommended Posts

From docwiki.embarcadero.com/Libraries/en/Data.DB.TDataSet.CopyFields

---------- schnipp ---------

Description

CopyFields copies the field definition structure of another dataset.


CopyFields copies the matching fields in the current records and returns the number of fields copied.

For more information, see http://qc.embarcadero.com/wc/qcmain.aspx?d=7768.

For another example, see http://community.devexpress.com/forums/p/47463/160009.aspx.

---------- schnapp ---------

 

So, which is it? Or does it do both?

This is no great help, especially since the first link results in an timeout error while the second says authorization required.

(The Delphi 2007 help was even less helpful: "This is CopyFields, a member of TDataset.")

 

OK, so I checked the sources and wasn't any wiser.

 

From the context in the sources I am currently looking at I guess it does the first, if necessary and then the second:

    DestDs.Append;
    DestDs.CopyFields(SrcDs);
    DestDs.Post;

But so far I am only guessing.

Share this post


Link to post

Intuitively (i.e. also guessing 😛), I would tend to believe that DestDs tries to look up fields that exists in it's own structure , by name from SrcDs, and copy the value of such fields? 

 

Share this post


Link to post

Looking at the function code, I would say that it copies the field values of the current record from the source data set to another data set. It also copies records of all nested data sets.

 

But I think there is a bug in their code: instead of 

CopyFields(TDataSetField(DestField).NestedDataSet);

should be 

TDataSetField(DestField).NestedDataSet.CopyFields(TDataSetField(SourceField).NestedDataSet);

Not tested yet.

Share this post


Link to post

OK, I'm pretty sure that it only copies the field values and then only for those fields that exist in the target dataset. It does not create new fields, so the description in the OLH is wrong:

 

* CopyFields copies the field definition structure of another dataset. WRONG

* CopyFields copies the matching fields in the current records and returns the number of fields copied.

Marco Cantu says the same in his Mastering Delphi Update for Delphi 2006 post.

 

Share this post


Link to post
Guest

@Kryvich, i think that when that line is called the recursion will treat the NestedDataSet as a DataSet.

So it will actually work.

What i mean is that for each nested record the function is using its own code in the else section.

However, to be 100% sure i would have to trace and check, and i never used nested datasets.

@dummzeuch, no structure is altered in that function, i agree. Field values existing in both sets will be copied.

The docs should say copies matching field values and...

Edited by Guest
Clarified the doc suggestion, last line

Share this post


Link to post
Guest

Also it will copy values "regardless" of field type matching or not, implicitly converting types, matching only on field names.

An EDataBaseError will be raised when types cannot be implicitly converted.

Share this post


Link to post
Guest

@Kryvich, exactly. And that is the case for the fields inside NestedDataSet. Because of recursion it will work. Property NestedDataSet is of type TDataSet and so on. Agree?

Share this post


Link to post

@Dany Marmur No, it does not work. You can see for yourself: I have prepared a test application with 2 in-memory datasets and 2 nested datasets (see TestCopyFields.zip in the attachment).

Values of fields of the nested dataset are not copied:

TestCopyField1.jpg.7ab2d3b6d6baee91a34233721613f423.jpg

But if you copy the Data.DB.pas to the application's folder and make the modification I suggested, it works:

TestCopyField2.jpg.9d9f6d9565fae929829a0886a0c76a6a.jpg

I think Embarcadero never tested this feature. Honestly, I myself learned about nested data sets only today.

TestCopyFields.zip

Share this post


Link to post
10 minutes ago, Kryvich said:

No, it does not work.

Please report that bug to QP and add your example program.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×