编辑:似乎 DataSetProvider 没有该项目所需的功能,因此我将实现一个自定义类来将数据加载到 ClientDataSet 中。
我正在尝试从连接到我的数据库的 TMSQuery 获取数据,并使用 DataSetProvider 用其中一些数据填充 ClientDataSet。
我的问题是,我需要修改其中一些数据,然后才能将其放入我的 ClientDataSet。 ClientDataSet 具有与原始数据库数据不匹配的持久字段。我什至无法将数据库中的字符串放入 ClientDataSet 中的备注字段中。
ClientDataSet 是我的数据层的一部分,因此我需要逐个字段将数据库中的数据与 ClientDataSet 保持一致(大多数都能够直接通过,但许多需要路由和/或转换) .
有人有这方面的经验吗?
最佳答案
您正在查找 TDataSetProvider.BeforeUpdateRecord 事件。为此事件编写一个事件处理程序,您可以手动控制如何将数据应用回数据库。
类似这样的事情
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;
关于delphi - DataSetProvider - DataSet 到 ClientDataSet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2548469/