Monday, November 26, 2007

Store Report Templates In Blob

How to store report templates in blob fields in database.
Answer:

Sometimes it is useful to keep report templates in blob fields in database. If you use EK RTF report component for Delphi you may use blob field to store report template in it.

Procedures below are using TBlobStream for input/output operations.

Store template in database:

procedure StoreTemplate(Fname:string);
var BS:TBlobStream;
begin
ReadTempFile(FName);
Table1.insert;
BS:=TBlobStream.create(Table1.FieldByName('Field1')as TBlobField,bmWrite);
BS.Write((PFile)^,flent);
Table1.Post;
Bs.Free;
end;

procedure ReadTempFile(FInFile:string);
var ActualRead : cardinal;
FileHandle: Integer;
SearchRec : TsearchRec;
size:integer;
flent:longint;
begin
flent:=-1;
if FindFirst(FInFile, faAnyFile, SearchRec)=0 then
begin
size:=SearchRec.Size;
GetMem(PFile,size+2);
FileHandle := FileOpen(FInFile, fmOpenRead);
ActualRead :=FileRead(FileHandle, PFile^, size);
FileClose(FileHandle);
flent:=ActualRead;
end;
if flent=0 then
begin showmessage('Can''t read file'+FInFile); end;
FindClose(SearchRec);
end;

Read template from database:

procedure ReadTemplate;
var BS:TBlobStream;
buffer:pointer;
size:longint;
begin
BS:=TBlobStream.create(Table1Field1,bmRead);
size:=BS.Seek(0,soFromEnd);
BS.Seek(0,soFromBeginning);
GetMem(Buffer,size);
BS.Seek(0,0);
BS.Read((Buffer)^,size);
BS.Free;
//Use method SetTemplateBuffer to assign template for EK RTF component
EKRTF1.SetTemplateBuffer(Buffer, Size);
end;

No comments: