Translate to:

Online Delphi Training

Learn LIVE
From The Comfort Of Your Home
Start Immediately!

+1 (678) 921-0644


Working With BLOBs Using Delphi

What is a BLOB?

BLOB is an acronym that stands for Large Binary Object, which is a collection of binary data stored in a data field in a database that supports this functionality. BLOBs are usually used to hold digitized multimedia content such as images, sound clips and videos. However, they can also be used to hold other types of data, such as executables, zip files, code and documents.

This example will show you how you can use Delphi to store BLOBs in a database and restore them to disk on demand.

Using BLOBs in Delphi

This example shows you how to work with BLOBs using Delphi. The above highlighted field
contains the name of a zip file that is stored as a BLOB in the same record. The zip file
contains the full source code, executable and database file that make up this project!
The image shown above is also stored as a BLOB in the same record.

 

What About BLOB?!

For our purposes, I will use a TClientDataSet to create a custom local database to store our BLOBs. You can use any database that supports BLOBs, such as SQL Server or Oracle. Note that not all databases support BLOBs.

The example will store a BMP image as a BLOB and display it in the application. In addition, it will have the option to store another BLOB in the same record, such as an executable, or a MS Word document. The record will also store the associated file names. You will be able to store these BLOBs and to restore them to a specified location on your drive.

I created five fields, using the Field Editor of the ClientDataSet. One is an auto-incremented ID field. Two are of type ftBlob and will hold our BLOBs, and two others are of type ftString to hold the associated file names. Since I do not need any undo functionality or have to apply against a database server, I have turned off logging with the LogChanges property to conserve system resources.

I use streams to read from a source file and write the result into a BLOB data field. On the ClientDataSet side, I call the function CreateBlobStream to obtain a stream for writing data to the BLOB field. By default, the FetchOnDemand property of the ClientDataSet is set to True. By calling CreateBlobStream you ensure that the current record has fetched its BLOB data. In the event that FetchOnDemand is set to False, you must call FetchBlobs before calling CreateBlobStream. The function CreateBlobStream expects two parameters: the BLOB field name and the mode which indicates whether the stream will be used for reading, writing, or updating the contents of the field. If you declare a variable "blob" as a TStream type and name the ClientDataSet as "cds" the following code is used to create the BLOB stream for writing to it:

blob := cds.CreateBlobStream(cds.FieldByName(FieldName), bmWrite);

The Seek function is called to move the position of the stream to the beginning of the resource on the disk and to return the value of the new position:

blob.Seek (0, soFromBeginning);

Next, the file stream has to be created to read from the source. If you declare a variable "myFileStream" as a TStream type, the following code does the job:

myFileStream := TFileStream.Create(FileName, fmShareDenyWrite);

Finally, you copy the data from the file stream into the BlobStream:

blob.CopyFrom(myFileStream, myFileStream.Size);

Following is the full procedure that stores the BLOB in the data field:

procedure TfrmMain.StoreBlob(FileName, FieldName: string);
var
blob, myFileStream : TStream;
begin
blob := cds.CreateBlobStream(cds.FieldByName(FieldName), bmWrite);
try
blob.Seek(0, soFromBeginning);
myFileStream := TFileStream.Create(FileName, fmShareDenyWrite);
try
blob.CopyFrom(myFileStream, myFileStream.Size) ;
finally
myFileStream.Free ;
end;
finally
blob.Free ;
end;
end;

Given the above explanation, the following procedure that restores the BLOB to the disk should be clear:

procedure TfrmMain.RestoreFile(FileName, FieldName: string);
var
blob : TStream;
begin
blob := cds.CreateBlobStream(cds.FieldByName(FieldName), bmRead);
try
blob.Seek(0, soFromBeginning);

with TFileStream.Create(FileName, fmCreate) do
try
CopyFrom(blob, blob.Size) ;
finally
Free ;
end;

finally
blob.Free ;
end;
end;

Download the full project to review the source code and to test run the included program and database.

 


The above example shows you how to use Delphi to store blobs in a database and then to restore them to disk. In our Delphi classes, we cover this in detail as part of the database programming classes.

Please contact us to find out more about our Delphi training. If you need consulting services, please call us at +1 678-921-0644 or visit us online at:

http://www.onlinedelphitraining.com/


We'd like to hear from you. Post Comments/Questions

Download Project: How To Use BLOBs With Delphi

Online Delphi Training


©2008 Business-IQ