2017年9月7日 星期四

將ClientDataset的資料寫入 AdoDataset裡指定的TableName

//設定維護表格, 經測試只有做Delete有作用, Update,Insert都會針對連結的表格做維護
F_RegADODataset[iIndex].Recordset.Properties['Unique Table'].Value := F_RegTableName[iIndex];


procedure pr_ImportData_With_BaseTableName(ATableName:String; AClientDataset:TClientDataset; AAdoDataset:TCustomAdoDataset);
var i, iIndex:Integer;
  sBaseTableName, sBaseColumnName, sFieldName:String;
  fdField:TField;
begin
  {將ClientDataset的資料寫入 AdoDataset裡指定的TableName}
  ATableName := LowerCase(ATableName);
  //
  with AClientDataset do
  begin
    try
      DisableControls;
      First;
      while not eof do
      begin
        AADODataset.Append;
        for i := 0 to FieldCount-1 do
        begin
          sFieldName := Fields[i].FieldName;
          fdField := AADODataset.FindField(sFieldName);
          if Assigned(fdField) then
          begin
            iIndex := fdField.Index;
            sBaseTableName := VarToStr(AADODataset.Recordset.Fields[iIndex].Properties['BASETABLENAME'].Value);
            sBaseTableName := LowerCase(sBaseTableName);
            sBaseColumnName := VarToStr(AADODataset.Recordset.Fields[iIndex].Properties['BASECOLUMNNAME'].Value);
            sBaseColumnName := LowerCase(sBaseColumnName);
            if (sBaseTableName=ATableName) and
               (sBaseColumnName=sFieldName) and
              (fdField.FieldKind=fkData) and
              (not fdField.ReadOnly)  then
            begin
              fdField.AsString := FieldByName(sFieldName).AsString;
            end;
          end;
        end;
        AADODataset.Post;
        //
        Next;
      end;
    finally
      EnableControls;
      First;
    end;
  end;
end;

沒有留言:

張貼留言