有没有更好的方法将所有DataSet字段及其属性复制到另一个DataSet? [英] Is there some better way to copy all DataSet Fields and their properties to another DataSet?

查看:141
本文介绍了有没有更好的方法将所有DataSet字段及其属性复制到另一个DataSet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在克隆一个TClientDataSet,我想将所有字段复制到该克隆(这是一个新的DataSet)中,我知道我可以遍历Fields并复制信息,或者制作我的类的2个实例,克隆光标,但是有更好的方法吗?比如创建一个新的数据集并分配字段信息?

I'm cloning a TClientDataSet and I want to copy all the fields to the clone (which is a new DataSet), I know I can loop through the Fields and copy the info, or make 2 instances of my class and just clone the cursor, but is there some better way? Something like create a new DataSet and assign the fields info?

编辑:

以下类帮助器方法对我有用:

The following class helper method works for me:

procedure TDataSetHelper.CopyFieldDefs(Source: TDataSet);
var
  Field, NewField: TField;
  FieldDef: TFieldDef;
begin
  for Field in Source.Fields do
  begin
    FieldDef := FieldDefs.AddFieldDef;
    FieldDef.DataType := Field.DataType;
    FieldDef.Size := Field.Size;
    FieldDef.Name := Field.FieldName;

    NewField := FieldDef.CreateField(Self);
    NewField.Visible := Field.Visible;
    NewField.DisplayLabel := Field.DisplayLabel;
    NewField.DisplayWidth := Field.DisplayWidth;
    NewField.EditMask := Field.EditMask;

   if IsPublishedProp(Field, 'currency')  then
     SetPropValue(NewField, 'currency', GetPropValue(Field, 'currency'));

  end;
end;

有人有更好的方法吗?

推荐答案

您是在寻找一种更美观的方式还是一种更快的方式?

Are you looking for a more aesthetic way of doing it or a faster way of doing it?

如果前者,创建自己的隐藏循环的类。

If the former, create your own classes that hide the loop.

如果是后者,甚至不必担心。一个非常明智的编码器曾经对我说过:磁盘访问成本;网络访问费用;屏幕访问费用;

If the latter, don't even worry about it. A very wise coder once said to me: disk access costs; network access costs; maybe screen access costs; everything else is free.

不要将源代码的大小与执行时间混淆。与数据库连接的初始握手相比,在内存中循环千次并复制位是不可检测的。

Don't confuse the size of the source code with execution time. Looping a thousand times through memory and copying bits is undetectable compared to the initial handshake of a database connection.

欢呼声

这篇关于有没有更好的方法将所有DataSet字段及其属性复制到另一个DataSet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆