请看如下代码:
procedure TMainForm.DtSBkyhqzdlrBeforeApplyUpdates(DataSet: TDataSet);
var
h: TTransActionHandle;
pDelta: TDeltaDataSet;
pField: TDataField;
begin
pDelta := TDeltaDataSet.Create(DataSet);
pField := pDelta.FieldByName('YHJLBH');
with DataSet do
try
h := Connection.Transaction.Start(False);
try
case pDelta.UpdateKind of
TUpdateKind.ukModify:
begin
if pDelta.FieldModified(pField) then
begin
Connection.ExecuteSQL('update yhjl set sfhs=''0'' where bh='''+ Variants.VarToStr(pField.OldValue) +'''');
Connection.ExecuteSQL('update yhjl set sfhs=''1'' where bh='''+ Variants.VarToStr(pField.NewValue) +'''');
end;
end;
TUpdateKind.ukInsert:
begin
Connection.ExecuteSQL('update yhjl set sfhs=''1'' where bh='''+ Variants.VarToStr(pField.NewValue) +'''');
end;
TUpdateKind.ukDelete:
begin
Connection.ExecuteSQL('update yhjl set sfhs=''0'' where bh='''+ Variants.VarToStr(pField.OldValue) +'''');
end;
end;
Connection.Transaction.Commit(h);
except
on E: Exception do
begin
Connection.Transaction.Rollback(h);
XTHSK.TXTHSK.ShowExcept(E.Message);
end;
end;
finally
pDelta.Free;
end;
end;