Monday, November 26, 2007

How To Use BLOB Type in Delphi

Many RDBMS usually complated with BLOB datatype. As far as I know there are many database supported blob already including the free version, which is firebird, mysql, postgresql, oracle and mssqlserver.

Blob type can be used to restore file such as image, e-book, exe file etc. And according to me, it can be used to make an update software version.

In delphi to save and load blob type is as the following:

//Save to blobstream and load
// To save a file to BLOB:
procedure TForm1.Button1Click(Sender: TObject);
var
//blob: TBlobStream;
blob, fs: TStream;
begin
blob := yourDataset.CreateBlobStream(yourDataset.FieldByName('YOUR_BLOB'), bmWrite);
try
blob.Seek(0, soFromBeginning);
fs := TFileStream.Create('c:\your_name.doc', fmOpenRead or
fmShareDenyWrite);
try
blob.CopyFrom(fs, fs.Size)
finally
fs.Free
end;
finally
blob.Free
end;
end;

// To load from BLOB:

procedure TForm1.Button1Click(Sender: TObject);
var
//blob: TBlobStream;
blob: TStream;
begin
blob := yourDataset.CreateBlobStream(yourDataset.FieldByName('YOUR_BLOB'), bmRead);
try
blob.Seek(0, soFromBeginning);

with TFileStream.Create('c:\your_name.doc', fmCreate) do
try
CopyFrom(blob, blob.Size)
finally
Free
end;
finally
blob.Free
end;
end;

2 comments:

Unknown said...

Hi,

I just wanted to say that I really enjoyed your blog and this post. You make some very informative points. Keep up the great work!

-
Delphi software development services

David Carter said...

This article of yours is very informative and interesting. You can also visit the article on morse decoder is one of the easiest and most recognized coded communication techniques. Thank you for sharing. Visit it and learn more.