How to Store Record Data in a BLOB Field in Delphi

Store Record Data,In Delphi, a report statistics type is a special type of person-defined records type. A report is a box for a aggregate of related variables of numerous sorts, referred to as fields, accrued into one kind.

In database applications, facts is saved in fields of numerous types: integer, string, bit (boolean), and so on. While most facts may be represented with easy information types, there are conditions whilst you need to store photographs, rich files or custom statistics sorts in a database. When this is the case you will use the BLOB (Binary Large Object) statistics type (“memo”, “ntext”, “photo”, and so on. – the call of the information kind depends at the database you figure with).

Store Record Data,Record as Blob

Here’s the way to save (and retrieve) a document (structure) value into a blob area in a database.

TUser = document … Suppose you have described your custom record kind as:

TUser = packed document Name : string[50]; CanAsk : boolean; NumberOfQuestions : integer; stop;

“Record.SaveAsBlob” To insert a brand new row (database record) in a database desk with a BLOB area named “statistics”, use the subsequent code:

var User : TUser; blobF : TBlobField; bs : TStream; begin User.Name := edName.Text; User.NumberOfQuestions := StrToInt(edNOQ.Text) ; User.CanAsk := chkCanAsk.Checked; myTable.Insert; blobF := myTable.FieldByName(‘information’) as TBlobField; bs := myTable.CreateBlobStream(blobF, bmWrite) ; try bs.Write(User,SizeOf(User)) ; subsequently bs.Free; give up; end;

In the code above:

  • “myTable” is the call of the TDataSet thing you are the usage of (TTable, TQuery, ADOTable, TClientDataSet, and so forth).
  • The call of the blob discipline is “information”.
  • The “User” variable (TUser document) is crammed using 2 edit packing containers (“edName” and “edNOQ”)and a take a look at field (“chkCanAsk”)
  • The CreateBlobStream approach creates a TStream item for writing to the blob subject.

“Record.ReadFromBlob” Once you have stored the record (TUser) information to a blob type subject, here’s how to “rework” binary information to a TUser cost:

var User : TUser; blobF : TBlobField; bs : TStream; start if myTable.FieldByName(‘information’).IsBlob then begin blobF := DataSet.FieldByName(‘information’) as TBlobField; bs := myTable.CreateBlobStream(blobF, bmRead) ; attempt bs.Read(user,sizeof(TUser)) ; finally bs.Free; stop; end; edName.Text := User.Name; edNOQ.Text := IntToStr(User.NumberOfQuestions) ; chkCanAsk.Checked := User.CanAsk; stop;

Note: the code above need to go within the “OnAfterScroll” event handler of the myTable dataset.

That’s it. Make positive you down load the pattern Record2Blob code.