Description

Often when manipulating datasets with similar structures, you need to copy the records from one dataset to another.  E.g. you may have fetched some records in a query or clientdaset and have located the matching records in another dataset.  You might then want to ensure the record values match without refetching the target dataset.
This looks like it would be easy to implement - i have rewritten it to take care of nested datasets as well as ordinary dataset fields.

Steps to Reproduce:

This is a requested new public method in the base TDataset class.
  1. function TDataSet.CopyFields(Source: TDataSet): Integer;
  2. // copies matching fields in current records- returns number of fields copied
  3. var
  4. FieldCtr: Integer;
  5. DestField, SourceField: TField;
  6. begin
  7. Result := ;
  8. for FieldCtr := to Source.FieldCount - do begin
  9. SourceField := Source.Fields[FieldCtr];
  10. Field := FindField(SourceField.FieldName);
  11. if not Assigned(Field) then Continue;
  12. if Field.ClassType = TDataSetField then begin // nested datasets
  13. while TDataSetField(Field).NestedDataSet.RecordCount > do
  14. TDataSetField(Field).NestedDataSet.Delete;
  15. TDataSetField(SourceField).NestedDataSet.First;
  16. while not TDataSetField(SourceField).NestedDataSet.Eof do begin
  17. TDataSetField(Field).NestedDataSet.Append;
  18. CopyFields(TDataSetField(Field).NestedDataSet, TDataSetField(SourceField).NestedDataSet);
  19. TDataSetField(Field).NestedDataSet.Post;
  20. TDataSetField(SourceField).NestedDataSet.Next;
  21. end;
  22. end else
  23. Field.Value := SourceField.Value;
  24. Inc(Result);
  25. end;
  26. end;
  27.  
  28. This would typically be used as follows:
  29.  
  30. SourceDS.First;
  31. while not SourceDS.EOF do begin
  32. if DestDS.Locate({info required to find matching record}) then begin
  33. DestDS.Edit;
  34. DestDS.CopyFields(SourceDS);
  35. DestDS.Post;
  36. end;
  37. SourceDS.Next;
  38. end;
 

TDataset.CopyFields的更多相关文章

  1. delphi之TDataset

    最近遇到了很多问题,现在做一下总结. 字符串处理: 字符串相加 var S1, S2: String; begin S1 := Concat('A', 'B'); // 连接两个字符串,S1变量等于A ...

  2. Delphi7数据库编程之TDataSet(转)

    TDataSet类由TBDEDataSet(BDE组件).TCustomADODataSet(ADO组件).TIBCustomDataSet(InterBase组件).TCustomSQLDataSe ...

  3. Delphi定位TDataSet数据集最后一条记录

    dst_temp.last ;//最后一条dst_temp.first ;//第一条dst_temp.next ;//下一条dst_temp.prior;//上一条

  4. 中间件序列TDATASET为BUFFER演示代码

    procedure SendStream(const AStream: TStream);var Buffer: array[0..4095] of Byte; // 每包最大4K StartPos, ...

  5. FireDACQuery FDQuery New

    FDQuery FDQuery1->ChangeCount;也有UpdatesPending属性 FDQuery1->ApplyUpdates() ExecSQL('select * fr ...

  6. JSON和数据集互相转换单元

    如题......只是一个单元, 为了测试JSON单元性能的... 具体测试结果参考: http://www.cnblogs.com/hs-kill/p/3668052.html 代码中用到的Seven ...

  7. DELPHI XE5 与SQLITE

    最近一次使用DELPHI做项目是使用DELPHI2009,为了访问本地数据库方便,使用ACCESS数据库,不需要安装驱动,(WINDOWS自带),但是ACCESS数据库的性能确实很糟糕,通过ADO连接 ...

  8. Datasnap Image

    delphi用,不能与java.c#互相识别. procedure TServerMethods.UpdateDoc(ItemID : integer; doc : TStream); delphi用 ...

  9. 多层数据库应用基于Delphi DataSnap方法调用的实现(一)返回数据集

    从Delphi 2009开始,DataSnap技术发生了很大的变化,并在Delphi 2010和Delphi XE的后续版本中得到了持续的改进.Delphi 2009之前的DataSnap,虽然也实现 ...

随机推荐

  1. Java中wait()和notify()方法的使用

    1. wait方法和notify方法 这两个方法,包括notifyAll方法,都是Object类中的方法.在Java API中,wait方法的定义如下: public final void wait( ...

  2. 棋盘分割(二维区间DP)

    题目大意:给一个棋盘,棋盘上每个格子中都有一个值,现在需要将棋盘切成n个矩形,总共切n-1刀,求最小的均方差.均方差定义为:,其中. 题目分析:将均方差化简得到:均方差2=(Σxi2)/n-平均值2. ...

  3. LeetCode 40

    // 既然不能重复利用,就在递归中选择下一个数,不能重复的话,就用setclass Solution { public: vector<vector<int>> combina ...

  4. Linux下weblogic启动报错unable to get file lock的问题

    非正常结束weblogic进程导致weblogic无法启动 由于先前服务器直接down掉了,所有进程都非正常的进行关闭了,也就导致了下次启动weblogic的时候报了以下错误:<2012-3-2 ...

  5. HDU 4686 Arc of Dream 矩阵快速幂,线性同余 难度:1

    http://acm.hdu.edu.cn/showproblem.php?pid=4686 当看到n为小于64位整数的数字时,就应该有个感觉,acm范畴内这应该是道矩阵快速幂 Ai,Bi的递推式题目 ...

  6. [转载]java正则表达式

    转载自:http://butter.iteye.com/blog/1189600 1.正则表达式的知识要点1.正则表达式是什么?正则表达式是一种可以用于模式匹配和替换的强有力的工具.2.正则表达式的优 ...

  7. I.MX6 linux kernel编译错误处理

    /******************************************************************************** * I.MX6 linux kern ...

  8. C#修改注册表

    某次需要使用C#对注册表进行操作,不过却发现没有权限,研究了以下发现是当前系统用户的问题.除非当前系统用户是Administrator,否则就会给你抛出一个异常.后来在网上发现了一个方法,原来C#也可 ...

  9. RabbitMQ核心概念

    AMQP的四个主要概念 1.虚拟主机(virtual host)或(vhost) 2.交换机(exchange) 3.队列(queue) 4.绑定器(bind) 什么是虚拟主机? 一组交换机.队列和绑 ...

  10. 重新学习之spring第三个程序,整合struts2+spring

    第一步:导入Struts2jar包+springIOC的jar包和Aop的Jar包 第二步:建立applicationContext.xml文件+struts.xml文件+web.xml文件 web. ...