关于BLOB字段与ORACLE中读写的新问题。(300分)

  • 主题发起人 huangyuansj
  • 开始时间
H

huangyuansj

Unregistered / Unconfirmed
GUEST, unregistred user!
我在BLOB字段与ORACLE中读写时,对文本文件和图象等其它数据可读可写,但对WORD和EXCEL
文件就不能读出,保存后的文件出现“文档的名称与路经无效,请检查文件或驱动器的权限!”
各位大侠请帮忙!!!

写程序如下:
var
filename,SfileName:string;
i,j:integer;
MFile:Tmemorystream;
begin
if not table1.Active then
Table1.Open;
Table1.Locate('documentno2',dbgrdconfig.DataSource.DataSet.FieldByName('DOCUMENTNO').AsString,[loCaseInsensitive]);
SfileName:=Table1.FieldByName('filename').AsString;
if SfileName<>'' then
begin
if Application.MessageBox('链接完成已存在,是否覆盖!','提示',MB_YESNO)=IDYES then
begin
ADOQuery1.Close;
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add('update safetyinformation2 set FileName='''',DocFile=empty_blob() where DocumentNo2='''+TRIM(DSSelect.DataSet.fieldbyname('DocumentNo').AsString)+'''' );
ADOQuery1.Prepared;
ADOQuery1.ExecSQL;
adoquery1.Close;
end
else
exit;
end;
OpenFile.InitialDir:=dmunit.ExePath;
i:=0;
if not openfile.Execute Then Exit;
Try
for j:=1 to length(openfile.FileName) do
if copy(openfile.FileName,j,1)='/' then i:=j;
filename:=copy(Openfile.FileName,i+1,length(openfile.FileName)-i);
MFile:=Tmemorystream.Create;
MFile.Position:=0;
MFile.LoadFromFile(openfile.FileName);
MFile.Position:=0;
if not table1.Active then
Table1.Open;
Table1.Locate('documentno2',dbgrdconfig.DataSource.DataSet.FieldByName('DOCUMENTNO').AsString,[loCaseInsensitive]);
dmunit.Database1.StartTransaction;
Table1.Edit;
Table1.FieldByName('filename').AsString:=filename;
TBlobField(table1.FieldByName('DocFile')).loadfromstream(Mfile);
// TBlobField(table1.FieldByName('DocFile')).LoadFromFile(openfile.FileName);
Table1.Post;
dmunit.Database1.Commit;
Application.MessageBox('建立链接完成!','提示',MB_OK);
Mfile.Free;
Except
Application.MessageBox('将文档实体保存到数据库时发生异常错误','保存失败',MB_OK+MB_ICONERROR);
raise;
End;
end;

读程序如下:

var
SFileName:string;
wordApp:Variant;
MyFile: TFileStream;
Stream: TBlobStream;
MemSize: Integer;
Buffer: PChar;
begin
if not table1.Active then
Table1.Open;
Table1.Locate('documentno2',dbgrdconfig.DataSource.DataSet.FieldByName('DOCUMENTNO').AsString,[loCaseInsensitive]);
SfileName:=Table1.FieldByName('filename').AsString;
if SfileName='' then
begin
Application.MessageBox('文档为空,不能显示','显示失败',MB_OK+MB_ICONERROR);
exit;
end;
if not DirectoryExists(dmunit.ExePath+'temp') then
CreateDir(dmunit.ExePath+'temp');
SfileName:=dmunit.ExePath+'temp/'+SfileName;
if FileExists(sFileName) then
DeleteFile(sFileName);
MyFile:=TFileStream.Create(SfileName,fmCreate);
Stream:=TBlobStream.Create(Table1.FieldByName('Docfile') as TBlobField, bmRead);
MemSize := Stream.Size;
Inc(MemSize);
Buffer := AllocMem(MemSize);
try
Stream.Read(Buffer^,MemSize);
MyFile.Write(Buffer^,MemSize);
finally
MyFile.Free;
Stream.Free;
FreeMem(Buffer);
end;
// TBlobField(Table1.FieldByName('docfile')).SaveToFile(sFileName);
if uppercase(copy(Sfilename,length(sfilename)-2,3))='DOC' then
begin
wordApp:= createOleObject('word.Application');
wordApp.Visible := True;
wordApp.Documents.open(sfilename);
// Wordapp.ConnectTo(WordApp.ActiveDocument);
end
else
ShellExecute(0, 'Open', PChar(sFileName), Nil, Nil, SW_SHOWNORMAL);

end;
 
顶部