中国程序员联盟 正在重新改版中ing 不便之处还请见谅 改版后将内容涉及java delphi .net php
 
  首页 | 数据库开发 | 网络通讯 | 多线程 | 多媒体开发 | 图像处理 | 程序人生 | 系统函数 | 控件开发 | Web服务
 
  当前位置:笨鱼delphi技术网>数据库开发>代码分析>文章内容

delphi 在DataSet间快速拷贝一笔记录

来源:站内 关于:bill 发布时间:2007-06-21   [收藏] [推荐]
  通常,你完成此项任务时使用 TBatchMove 构件。但 TBatchMove 也有不合适的情况:
  - 你并不希望拷贝所有的记录,也不想使用过滤器。
  - 你希望用到目的 DataSet 的有效事件(如 BeforPost,OnValidate,等)。
  - 或者更坏的情况,两 DataSet 的结构并不相同。
  使用下面的简单过程,上面的问题都可以被解决,所有字段中的数据将被拷贝到另外一个 Dataset 中的同名字段中。但这里面也存在一些限制:
  - 不能拷贝查找和计算字段
  - 当存在相同字段名但数据类型不同时,你需要先使用 Assign 判断。
  - 当然,目的字段不能是只读的。
  
  procedure CopyRecord(Source, Destination: TDataSet); 
  var Ind:longint; 
      SField, DField: TField; 
  begin 
    for Ind:=0 to Source.FieldCount - 1 do 
     begin 
       SField := Source.Fields[ Ind ]; 
       DField := Destination.FindField( SField.FieldName ); 
       if (DField <> nil) and (DField.FieldKind = fkData) and 
          not DField.ReadOnly then 
          if (SField.DataType = ftString) or 
             (SField.DataType <> DField.DataType) then 
             DField.AsString := SField.AsString 
          else 
             DField.Assign( SField ) 
     end; 
  end; 
  
  旧方法:
  
    DEST.Open; 
    ORIGIN.Open; 
    while not ORIGIN.Eof do 
    begin 
      if ORIGINTYPE.AsString = 'T' then 
        with ORIGIN do 
        begin 
          DEST.Append; 
          DEST.FieldByName('TYPE').AsString := ORIGINTYPE.AsString; 
          DEST.FieldByName('FIRSTNAME').AsString := ORIGINFIRSTNAME.AsString; 
          DEST.FieldByName('LASTNAME').AsString := ORIGINLASTNAME.AsString; 
          DEST.FieldByName('CPF').AsString := ORIGINCPF.AsString; 
          DEST.FieldByName('PARTY').AsString := ORIGINPARTY.AsString; 
          DEST.Post; 
        end; 
       ORIGIN.Next; 
    end; 
  
  使用该过程的调用示范:
  
    DEST.Open; 
    ORIGIN.Open; 
    while not ORIGIN.Eof do begin 
      if ORIGINTYPE.AsString = 'T' then begin 
         DEST.Append; 
         CopyRecord( ORIGIN, DEST );         
         DEST.Post; 
      end; 
      ORIGIN.Next; 
    end; 

[浏览: 次]   
上一篇:delphi 输出TDataset中的内容至逗号侵害的ASCII文件   下一篇:delphi 如何使用Bookmarks记录表中的先前状态
[收藏] [推荐] [返回顶部] [打印本页] [关闭窗口]  
    评论加载中…
google adsense热点文章
·delphi Delphi_动态数组介绍----Delphi
·delphi 检查IP地址
·delphi DBExpress 提高
·delphi 复制Excel列到二维数组
·delphi Delphi_VCL消息处理机制的内幕
·delphi 产生不重复的随机数
·delphi 如何在DELPHI里调用MS office
·delphi 缓冲池
·delphi Asta多层应用实现
·delphi MIDAS——多层分布式应用程序服
·delphi 在WORD文档里添加页签
·delphi Datamodule的应用方式
     delphi技术网 | firefox 下载 | Avant Browser下载 | dedecms 技术网 | drupal 爱好者 | php 技术网
  Copyright@www.delphichm.com,2006-2009.All Rights Reserved.
 
程序员联盟 | delphi Java .net|QQ:707102932