Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   OOP (https://www.clubdelphi.com/foros/forumdisplay.php?f=5)
-   -   Copiar Pegar registros (https://www.clubdelphi.com/foros/showthread.php?t=94502)

oscarac 06-03-2020 22:59:49

Copiar Pegar registros
 
buenas tardes
no se si habrá una función que haga lo que estoy planteando

muchas veces se me ha presentado la oportunidad tarea de trabajar con ClientDataSet que tiene casi la misma estructura que una tabla (digamos que para la carga masiva de registros en un temporal y luego su posterior grabación fisica)

existe alguna función que permita copiar en memoria los registros del ClientDataSet y grabarlos en la tabla fisica? y viceversa de la tabla física al ClientDataSet

aquellos que conocen Foxpro, es algo similar al comando Scatter Memvar y Gather Memvar

movorack 06-03-2020 23:47:37

Este código es originario del compañero [gatosoft], depronto te sirva

Código Delphi [-]
  TDatasetHelper = class helper for TDataSet
  Private
  Public
    procedure CopyCurrentRecord(DataSet: TDataset; IgnoreFieldNames: array of string); overload;
    procedure CopyCurrentRecord(DataSet: TDataset); overload;
    procedure CopyData(DataSet: TDataset; IgnoreFieldNames: array of string); overload;
    procedure CopyData(DataSet: TDataset); overload;  
  end;

procedure TDatasetHelper.CopyCurrentRecord(DataSet: TDataset);
begin
  CopyCurrentRecord(DataSet, []);
end;

procedure TDatasetHelper.CopyCurrentRecord(DataSet: TDataset;
  IgnoreFieldNames: array of string);
  function IgnoreField(FieldName: string): Boolean;
    var
      lFieldName : String;
  begin
    Result := False;
    for lFieldName in IgnoreFieldNames do
    begin
      Result := SameText(FieldName.Trim, lFieldName.trim);
      if Result then
        Break;
    end;
  end;
  var
    lField: TField;
begin
  if (not Dataset.Active) or Dataset.IsEmpty then
    Exit;

  if not (Self.State in [dsEdit, dsInsert]) then
    if Self.IsEmpty then
      Self.Append
    else
      Self.Edit;

  for lField in Self.Fields do
  begin
    if IgnoreField(lField.FieldName) then
      Continue;

    if not Assigned(DataSet.FindField(lField.FieldName)) then
      Continue;

    if lField.IsBlob then
    begin
      try
        //Se intenta realizar la copia
        lField.AsBytes := DataSet.FieldByName(lField.FieldName).AsBytes
      except
      end;
    end
    else
    if (lField.Value <> DataSet.FieldByName(lField.FieldName).Value) then
      lField.Value := DataSet.FieldByName(lField.FieldName).Value;
  end;
end;

procedure TDatasetHelper.CopyData(DataSet: TDataset);
begin
  CopyData(DataSet, []);
end;

procedure TDatasetHelper.CopyData(DataSet: TDataset; IgnoreFieldNames: array of string);
  var
    BookMark: TBookmark;
begin
  if (not DataSet.Active) or DataSet.IsEmpty then
    Exit;

  if Self.State in [dsEdit, dsInsert] then
    Self.Cancel;

  BookMark := nil;
  try
    DataSet.DisableControls;
    Self.DisableControls;
    BookMark := DataSet.GetBookmark;

    DataSet.First;
    while not DataSet.Eof do
    begin
      Self.Append;
      CopyCurrentRecord(Dataset, IgnoreFieldNames);
      Self.Post;
      Dataset.Next;
    end;

    Self.First;
  finally
    if Assigned(BookMark) and Dataset.BookmarkValid(BookMark) then
      DataSet.GotoBookmark(BookMark);

    DataSet.EnableControls;
    Self.EnableControls;
  end;
end;


La franja horaria es GMT +2. Ahora son las 10:24:07.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi