DataSetProvider - DataSet for ClientDataSet

EDIT: It seems that the DataSetProvider does not have the necessary functions for this project, so I will implement a special class for loading data into the ClientDataSet.

I am trying to take data from a TMSQuery connected to my database and populate the ClientDataSet with some of this data using a DataSetProvider.

My problem is that I will need to modify some of this data before it can enter my ClientDataSet. ClientDataSet has persistent fields that do not match raw database data. I can’t even get a row from the database in the memo field in ClientDataSet.

ClientDataSet is part of my data layer, so I will need to map the data from the database to the ClientDataSet field by field (most of them will be able to go straight through, but many of them will require routing and / or conversion).

Does anyone have any experience?

+3
source share
3 answers

You are looking for the TDataSetProvider.BeforeUpdateRecord event. Write an event handler for this event, and you can manually control how the data will be applied back to the database.

Something like that

procedure TDataModule1.DataSetProvider1BeforeUpdateRecord(Sender: TObject; SourceDS: TDataSet; DeltaDS: TCustomClientDataSet; UpdateKind: TUpdateKind; var Applied: Boolean);
begin
  { Set applied to tell DataSnap that you have applied this record yourself }
  Applied := True;

  case UpdateKind of
    ukModify:
      begin
        Table1.Edit;
        { set the values of the fields something like this }
        if not VarIsEmpty(DeltaDS.FieldByName('NewValue')) then
          Table1['SomeField'] := DeltaDS.FieldByName('SomeField').NewValue;
        Table1.Post;
    end;

    ukInsert:
      begin
        Table1.Insert;
        { set the values of the fields }
        Table1['SomeField'] := DeltaDS['SomeField']
        Table1.Post;
      end;

    ukDelete:
      if Table1.Locate('PrimaryKeyField', DeltaDS['PrimaryKeyField'], []) then
        Table1.Delete;
  end; // case
end;
+2
source

, ClientDataSet , , TQuery, , . , , , , TQuery.

TQuery.

ClientDataSet, , . , Google "TClientDataSet ". , , , . , OnUpdateRecord , , .

0

, ClientDataSet, TDataSetProvider.OnGetData.

procedure TDataModule1.DataSetProvider1GetData(Sender: TObject; DataSet: TCustomClientDataSet);
begin
  DataSet.First;
  while not DataSet.Eof do begin
    DataSet.Edit;
    DataSet['Surname'] := UpperCase(DataSet['Surname']);
    DataSet.Post;
    DataSet.Next;
  end; // while
end;

ClientDataSet TDataSetProvider.OnUpdateData. OnGetData, , .

procedure TDataModule1.DataSetProvider1UpdateData(Sender: TObject; DataSet: TCustomClientDataSet);
begin
  DataSet.First;
  while not DataSet.Eof do begin
    DataSet.Edit;
    DataSet['Surname'] := LowerCase(DataSet['Surname']);
    DataSet.Post;
    DataSet.Next;
  end; // while
end;

This OnUpdateData event is raised before the OnBeforeUpdateRecord event. Also, the OnGetData and OnUpdateData events work on the entire data set, and OnBeforeUpdateRecord is called once for each changed record.

0
source

Source: https://habr.com/ru/post/1739204/


All Articles