不用控件的话可以这样做
程序如下:
procedure TsampleForm.OpenDOCClick(Sender: TObject);
var
MemSize: Integer;
Buffer: PChar;
MyFile: TFileStream;
Stream: TBlobStream;
begin
OpenDialog1.Filter:='WORD文档(*.DOC)|*.DOC'
;{从对话窗选择文件}
if OpenDialog1.Execute then begin
MyFile:=TFileStream.Create
(OpenDialog1.FileName,fmOpenRead);
with table1 do {‘table1’为含BLOB字段的表名}
begin
Open;
Edit;
Stream := TBlobStream.Create(FieldByName('Doc')
as TBlobField, bmWrite);
{‘Doc’为BLOB字段名}
MemSize := MyFile.Size;
Inc(MemSize); {Make room for the
buffer's null terminator.}
Buffer := AllocMem(MemSize);
{Allocate the memory.}
try
Stream.Seek(0, soFromBeginning);
{Seek 0 bytes from the stream's end point}
MyFile.Read(Buffer^,MemSize);
Stream.Write(Buffer^,MemSize);
finally
MyFile.Free;
Stream.Free;
end;
try
Post;
except
on E: EDatabaseError do
if HandelException(E)< >0 then exit
else
raise;
end;
end;
Doc_ole.CreateObjectFromFile
(OpenDialog1.FileName,False);
Doc_ole.Run;{Doc_ole为ToleContainer构件名}
end;
end;
---- 以上为向数据库中写入的程序,应用中从对话窗取出文件在
ToleContainer构件中显示的同时存入数据库。
procedure TsampleForm.GetDocClick
(Sender: TObject);
var
MemSize: Integer;
Buffer: PChar;
MyFile: TFileStream;
Stream: 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');
Doc_ole.CreateObjectFromFile
('c:/temp.DOC',False);
Doc_ole.Run;
end;
end;
---- 以上程序为从数据库从将WORD文档取出,并放在temp.doc的临时文件上
并在ToleContainer构件中显示。
---- 在程序的其他部份应准确控制表记录指针,使WORD文档的存取发生在正确
的记录位置。