zoukankan      html  css  js  c++  java
  • TDataset.CopyFields

    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.
    function TDataSet.CopyFields(Source: TDataSet): Integer;
    // copies matching fields in current records- returns number of fields copied
    var
      FieldCtr: Integer;
      DestField, SourceField: TField;
    begin
      Result := 0;
      for FieldCtr := 0 to Source.FieldCount - 1 do begin
        SourceField := Source.Fields[FieldCtr];
        Field := FindField(SourceField.FieldName);
        if not Assigned(Field) then Continue;
        if Field.ClassType = TDataSetField then begin  // nested datasets
          while TDataSetField(Field).NestedDataSet.RecordCount > 0 do
            TDataSetField(Field).NestedDataSet.Delete;
          TDataSetField(SourceField).NestedDataSet.First;
          while not TDataSetField(SourceField).NestedDataSet.Eof do begin
            TDataSetField(Field).NestedDataSet.Append;
            CopyFields(TDataSetField(Field).NestedDataSet, TDataSetField(SourceField).NestedDataSet);
            TDataSetField(Field).NestedDataSet.Post;
            TDataSetField(SourceField).NestedDataSet.Next;
          end; 
        end else
          Field.Value := SourceField.Value;
        Inc(Result);
      end;
    end;
    
    
    This would typically be used as follows:
    
    SourceDS.First;
    while not SourceDS.EOF do begin
      if DestDS.Locate({info required to find matching record}) then begin
        DestDS.Edit;
        DestDS.CopyFields(SourceDS);
        DestDS.Post;
      end;
      SourceDS.Next;
    end; 
     
  • 相关阅读:
    APP移动端开发html模板
    javascript使用百度地图api和html5特性获取浏览器位置
    javascript与jQuery的each,map回调函数参数顺序问题
    fiddler操作改到本地
    做54活动总结
    移动端文字垂直居中问题兼容
    ADO。Net(二)——防止SQL注入攻击
    ADO.Net(一)——增、删、改、查
    面向对象(六)——设计模式和委托
    面向对象(五)——类库和五大原则
  • 原文地址:https://www.cnblogs.com/cb168/p/4269551.html
Copyright © 2011-2022 走看看