Using BLOB fields in Delphi datasets

Technically, BLOB fields are a special kind of fields in a dataset that can store a binary large object (BLOB) : raw binary data of arbitrary length. These data are in origin a picture or a file, transformed to Base64 code and stored into dataset’s records.

In Delphi (represented by TBlobField class), you can use this kind of fields in any dataset in both frameworks (VCL and FMX), to store and retrieve a picture or file in runtime.

To view BLOB fields graphical content (like a stored image), they can be linked to any image control using one of these methods :

  • Data-aware controls (like TDBImage).
  • By Livebindings to any image control.
  • Using the bitmap property of a control.

In this tutorial, we will show you some code snippets for an optimal use of BLOB fields :

  • Load a file into a BLOB field
  • Save a file from a BLOB field
  • Copy one BLOB field content from a dataset to another dataset
  • Copy all BLOB fields content of a dataset to another dataset using a loop

Suppose you have a dataset control named ‘FDMemTable1’, with a BLOB field named ‘MyBlobField1’. And a second dataset control named ‘FDMemTable2’, with a BLOB field named ‘MyBlobField2’

Load a file into a BLOB field

var
FileStream : TFileStream;
BlobStream : TStream;

begin
if (OpenDialog1.Execute) then
   begin
   FDMemTable1.Edit;
   try
      BlobStream := FDMemTable1.CreateBlobStream(TableUniversity.FieldByName('MyBlobField1'), bmWrite);
      FileStream := TFileStream.Create(OpenDialog1.FileName, fmOpenRead or fmShareDenyNone);
      BlobStream.CopyFrom(FileStream, FileStream.Size);
      FileStream.Free;
      BlobStream.Free;
      FDMemTable1.Post;
   except
      FDMemTable1.Cancel;
   end;
end;
end;

Save a file from a BLOB field

var
FileStream : TFileStream;
BlobStream : TStream;

begin
if (SaveDialog1.Execute) then
begin
   FileStream := TFileStream.Create(SaveDialog1.FileName, fmCreate);
   BlobStream := FDMemTable1.CreateBlobStream(TableQCM.FieldByName('MyBlobField1'), bmRead);
   FileStream.CopyFrom(BlobStream, BlobStream.Size-BlobStream.Position);
   BlobStream.Free;
   FileStream.Free;
end;
end;

Copy one BLOB field content from a dataset to another dataset

var
SourceStream : TStream;
BlobStream : TStream;

begin
FDMemTable2.Edit;
try
   SourceStream := FDMemTable1.CreateBlobStream(FDMemTable1.FieldByName('MyBlobField1'), bmRead);
   BlobStream := FDMemTable2.CreateBlobStream(FDMemTable2.FieldByName('MyBlobField2'), bmWrite);
   BlobStream.CopyFrom(SourceStream, SourceStream.Size);
   SourceStream.Free;
   BlobStream.Free;
   FDMemTable2.Post;
except
   FDMemTable2.Cancel;
end;
end;

Copy all BLOB fields content of a dataset to another dataset using a loop

var
I : integer;
SourceStream : TStream;
BlobStream : TStream;

begin
for I := 1 to FDMemTable1.RecordCount do
begin
FDMemTable2.Edit;
try
   SourceStream := FDMemTable1.CreateBlobStream(FDMemTable1.FieldByName('MyBlobField1'), bmRead);
   BlobStream := FDMemTable2.CreateBlobStream(FDMemTable2.FieldByName('MyBlobField2'), bmWrite);
   BlobStream.CopyFrom(SourceStream, SourceStream.Size);
   SourceStream.Free;
   BlobStream.Free;
   FDMemTable2.Post;
except
   FDMemTable2.Cancel;
end;
FDMemTable1.Next;
end;
end;

Leave a Reply