Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
496 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (0)


Dataset Row ChecksumFormat this article printer-friendly!Bookmark function is only available for registered users!
calculate a checksum
Product:
Delphi 5.x (or higher)
Category:
DB-General
Skill Level:
Scoring:
Last Update:
07/15/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet checksum dataset GetDataSize GetData xor
Times Scored:
1
Visits:
4069
Uploader: Andreas Schmidt
Company:
Reference: N/A
 
Question/Problem/Abstract:
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;






Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment













 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
M. Maes
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)