ADO Database Delphi

Title: Dataset Row Checksum
Question: Sometimes you want to detect changes in your
tables.
This could be archived with a checksum for every row of your table.
Update or Inserts on the table outside your app can be detected.
Answer:
- add a Integerfield named "CheckSum" to your table
- call the following function in the "BeforePost" event of your table/Query
procedure TDataModule1.Table1BeforePost(Dataset:TDataset);
begin
DataSet['CheckSum'] := DataSetCheckSum(Dataset, 'CheckSum');
end;
- validate the correctness of your data
if DataSetCheckSum(Table1, '') 0 then
raise Exception.Create('the Table was modified outside this appllication!');
You may want to call DataSetCheckSum in the Event OnDrawColumnCell
of your DBGrid.
if DataSetCheckSum((Sender as TDBGrid).DataSource.DataSet, '') 0 then
DBGrid1.Canvas.Color := clRed;
function DataSetCheckSum(ds: TDataSet; const chkFieldname: string): integer;
type
Pinteger = ^integer;
var
i, size: integer;
data: string;
x: PChar;
field : TField;
begin
Result := 0;
if not ds.Active then
begin
Exit;
end;
data := '';
for i := 0 to ds.FieldCount - 1 do
begin
field := ds.Fields.Fields[i];
if CompareStr(field.FieldName, chkFieldname) 0 then
begin
size := field.DataSize;
if size 0 then
begin
// workaround for bug in TWideStringField.GetDataSize
if field.DataType = ftWidestring then
begin
data := data + field.AsString
end
else
begin
SetLength(data, Length(data)+size);
x := PChar(data)+Length(data)-size;
field.GetData(x);
end;
end;
end;
end;
// Length of data must be a multiple of 4
data := data + StringOfChar(#0, 4 - (Length(data) mod 4));
// calculate the checksum
x := PChar(data);
for i := Length(data) div 4 downto 0 do
begin
Result := Result xor (Pinteger(x)^);
Inc(x, SizeOf(integer));
end;
end;