下面的程序对你或许有帮助
procedure TsampleForm.OpenDOCClick(Sender: TObject);
var
MemSize: Integer;
Buffer: PChar;
fStream: TFileStream;
bStream: TBlobStream;
begin
OpenDialog1.Filter:='WORD文档(*.DOC)|*.DOC';{从对话窗选择文件}
if OpenDialog1.Execute then
begin
fStream:=TFileStream.Create(OpenDialog1.FileName,fmOpenRead);
with table1 do
{'table1'为含BLOB字段的表名}
begin
Open;
Edit;
bStream := TBlobStream.Create(FieldByName('Doc') as TBlobField, bmWrite);
{'Doc'为BLOB字段名}
MemSize := fStream.Size;
Inc(MemSize);
{Make room for the buffer's null terminator.}
Buffer := AllocMem(MemSize);
{Allocate the memory.}
try
bStream.Seek(0, soFrombegin
ning);
{Seek 0 bytes from the stream's end point}
fStream.Read(Buffer^,MemSize);
bStream.Write(Buffer^,MemSize);
finally
fStream.Free;
bStream.Free;
end;
try
Post;
except
on E: EDatabaseError do
if HandelException(E)< >0 then
exit
else
raise;
end;
end;
do
c_ole.CreateObjectFromFile(OpenDialog1.FileName,False);
do
c_ole.Run;{Doc_ole为ToleContainer构件名}
end;
end;
---- 以上为向数据库中写入的程序,应用中从对话窗取出文件在ToleContainer
构件中显示的同时存入数据库。
procedure TsampleForm.GetDocClick(Sender: TObject);
var
MemSize: Integer;
Buffer: PChar;
fStream: TFileStream;
bStream: TBlobStream;
begin
MyFile:=TFileStream.Create('c:/temp.tmp',fmCreate);
with Query1 do
begin
Stream := TBlobStream.Create(FieldByName('Doc') as TBlobField, bmRead);
MemSize := Stream.Size;
Inc(MemSize);
{Make room for the buffer's null terminator.}
Buffer := AllocMem(MemSize);
{Allocate the memory.}
try
Stream.Read(Buffer^,MemSize);
MyFile.Write(Buffer^,MemSize);
finally
MyFile.Free;
Stream.Free;
end;
end;
if FileExists('c:/temp.DOC') then
DeleteFile('c:/temp.DOC');
if FileExists('c:/temp.tmp') then
begin
RenameFile('c:/temp.tmp', 'c:/temp.DOC');
do
c_ole.CreateObjectFromFile('c:/temp.DOC',False);
do
c_ole.Run;
end;
end;
---- 以上程序为从数据库从将WORD文档取出,并放在temp.doc的临时文件上并在
ToleContainer构件中显示。