字段上没有存储新值/旧值信息。增量将包含 1 行用于删除的行 1 行用于新插入的行 2 行用于修改的行
对于每一行,您可以询问delta.UpdateStatus
哪些可以是 usUnmodified、usModified、usInserted 或 usDeleted。
每个未修改的记录后面都有一个带有修改的记录。
您将查看两条记录以获取旧值和新值。
procedure TTestForm.RunInfoClick(Sender: TObject);
type
TMyFieldInfo = Record
FieldName: String;
Size: Integer;
DataType: TFieldType;
FieldKind: TFieldKind;
end;
var
I: Integer;
sl: TStringList;
old: String;
FA: Array of TMyFieldInfo;
F: TField;
// get fielddefs after openening and a a definition for a calculated field
// called Status to refect UpdateStatus
Procedure GetFields;
var
I: Integer;
begin
SetLength(FA, delta.FieldCount + 1);
for I := 0 to delta.FieldCount - 1 do
begin
FA[I].FieldName := delta.Fields[I].FieldName;
FA[I].DataType := delta.Fields[I].DataType;
FA[I].Size := delta.Fields[I].Size;
FA[I].FieldKind := fkdata;
end;
FA[High(FA)].FieldName := 'Status';
FA[High(FA)].DataType := ftString;
FA[High(FA)].Size := 10;
FA[High(FA)].FieldKind := fkcalculated;
delta.Close;
end;
// apply our fields to be able to display a calculated field
Procedure SetFields;
var
I: Integer;
begin
delta.Fields.Clear;
for I := Low(FA) to High(FA) do
begin
F := DefaultFieldClasses[FA[I].DataType].Create(delta);
With F do
begin
FieldName := FA[I].FieldName;
FieldKind := FA[I].FieldKind;
Size := FA[I].Size;
DataSet := delta;
end;
end;
delta.Open;
end;
Procedure LogSL;
begin
if sl.Count > 1 then
Memo1.Lines.Add(sl.Text);
sl.Clear;
end;
begin
Memo1.Lines.Clear;
sl := TStringList.Create;
try
delta.Close;
delta.Fields.Clear;
delta.Data := ClientDataSet1.delta;
GetFields;
SetFields;
while not delta.Eof do
begin
if delta.UpdateStatus <> usModified then
begin
LogSL;
end;
if delta.UpdateStatus = usUnmodified then
sl.Add('Unmodified:')
else if delta.UpdateStatus = usInserted then
begin
sl.Add('Insert:');
end
else if delta.UpdateStatus = usDeleted then
begin
sl.Add('Deleted:');
end
else if delta.UpdateStatus = usModified then
begin
sl[0] := ('Modified:');
end;
for I := 0 to delta.FieldCount - 2 do // ignore our calculated field
begin
if delta.UpdateStatus = usModified then
begin
if (sl.Values[delta.Fields[I].FieldName] <> delta.Fields[I].AsString) and not delta.Fields[I].IsNull then
begin // we had changes
sl[I + 1] := sl[I + 1] + ' OldValue: ' + delta.Fields[I].AsString;
end
else
begin // we did not have changes take stored OldValue
sl[I + 1] := sl[I + 1] + ' OldValue: ' + sl.Values[delta.Fields[I].FieldName];
end
end
else // delta.UpdateStatus = usModified
sl.Add(delta.Fields[I].FieldName + '=' + delta.Fields[I].AsString + old);
end;
delta.Next;
end;
LogSL;
finally
sl.Free;
end;
end;
procedure TTestForm.deltaCalcFields(DataSet: TDataSet);
begin
with TClientDataSet(DataSet) do
begin
case UpdateStatus of
usUnmodified:
FieldByName('Status').AsString := 'Unmod';
usModified:
FieldByName('Status').AsString := 'Modi';
usInserted:
FieldByName('Status').AsString := 'Ins';
usDeleted:
FieldByName('Status').AsString := 'Del';
end;
end;
end;