W
watter
Unregistered / Unconfirmed
GUEST, unregistred user!
本人在数据库中建立一个表:tblTemplate,存放一些Word模板,在程序中取出模板的内容存放到临时文件中,
用OleContainer显示摸版内容,用户填入适当的内容后再保存到数据库的其他表中;出现如下问题:
1、用Tdatabase+Ttable时一切正常,可以取出并显示;
2、当用TDCOMConnection+TClientDataSet时出现问题;建立BlobStream时提示错误“Invalid Class typecast”
部分代码如下:
procedure TForm1.btnGetTemplateClick(Sender: TObject);
var
MemSize: Integer;
Buffer: PChar;
TempFileStream: TFileStream;
Stream: TBlobStream;
begin
CurrentPath:=ExtractFilepath(Application.Exename);
TempPath:=CurrentPath+'Temp/';
if not DirectoryExists(TempPath) then
MkDir(TempPath);
with cdsTemplatedo
//如果换成tblTemplate(Ttable)则正常
begin
Close;
Open;
First;
while not eofdo
begin
TempFile:=TempPath+FieldByName('Filename').AsString+FieldByName('Extname').AsString;
// Filename:文件名;Extname:文件扩展名;
TempFileStream:=TFileStream.Create(TempFile,fmCreate);
Stream := TBlobStream.Create(FieldByName('Filecontent') as TBlobField, bmRead);
//上面一条语句出错:'Invalid class typecast'
MemSize := Stream.Size;
Inc(MemSize);
{Make room for the buffer's null terminator.}
Buffer := AllocMem(MemSize);
{Allocate the memory.}
try
Stream.Read(Buffer^,MemSize);
TempFileStream.Write(Buffer^,MemSize);
finally
TempFileStream.Free;
Stream.Free;
end;
Next;
end;
end;
end;
用OleContainer显示摸版内容,用户填入适当的内容后再保存到数据库的其他表中;出现如下问题:
1、用Tdatabase+Ttable时一切正常,可以取出并显示;
2、当用TDCOMConnection+TClientDataSet时出现问题;建立BlobStream时提示错误“Invalid Class typecast”
部分代码如下:
procedure TForm1.btnGetTemplateClick(Sender: TObject);
var
MemSize: Integer;
Buffer: PChar;
TempFileStream: TFileStream;
Stream: TBlobStream;
begin
CurrentPath:=ExtractFilepath(Application.Exename);
TempPath:=CurrentPath+'Temp/';
if not DirectoryExists(TempPath) then
MkDir(TempPath);
with cdsTemplatedo
//如果换成tblTemplate(Ttable)则正常
begin
Close;
Open;
First;
while not eofdo
begin
TempFile:=TempPath+FieldByName('Filename').AsString+FieldByName('Extname').AsString;
// Filename:文件名;Extname:文件扩展名;
TempFileStream:=TFileStream.Create(TempFile,fmCreate);
Stream := TBlobStream.Create(FieldByName('Filecontent') as TBlobField, bmRead);
//上面一条语句出错:'Invalid class typecast'
MemSize := Stream.Size;
Inc(MemSize);
{Make room for the buffer's null terminator.}
Buffer := AllocMem(MemSize);
{Allocate the memory.}
try
Stream.Read(Buffer^,MemSize);
TempFileStream.Write(Buffer^,MemSize);
finally
TempFileStream.Free;
Stream.Free;
end;
Next;
end;
end;
end;