delphi中处理OleVariant和TStream
来源:互联网 发布:淘宝2016促销活动时间 编辑:程序博客网 时间:2024/06/01 07:59
因为客户不知道从数据库中流字段到打开文档,编辑,再保存到数据库中流字段这个流程,这个流程涉及到了接口OpenDocumentWithStream和SaveToStream,而对应的VARIANT类型在delphi中展现为OleVariant,于是中间涉及到OleVariant转为TStream以及TStream转换为OleVariant,我写的一个例子如下:
function TForm1.OleVariantToMemoryStream(OV: OleVariant): TMemoryStream;
var
Data: PByteArray;
Size: integer;
begin
Result := TMemoryStream.Create;
try
Size := VarArrayHighBound (OV, 1) - VarArrayLowBound(OV, 1) + 1;
if Size = 0 then
begin
if Result <> nil then Result.Free;
Result := nil;
exit;
end;
Data := VarArrayLock(OV);
try
Result.Position := 0;
Result.WriteBuffer(Data^, Size);
finally
VarArrayUnlock(OV);
end;
except
VarArrayUnlock(OV);
Result.Free;
Result := nil;
end;
end;
function TForm1.StreamToVariant(Stream: TStream): OleVariant;
var
p: Pointer;
begin
Result := VarArrayCreate([0, Stream.Size - 1], varByte);
p := VarArrayLock(Result);
try
Stream.Position := 0;
Stream.Read(p^, Stream.Size);
finally
VarArrayUnlock(Result);
end;
end;
调用OpenDocumentWithStream
procedure TForm1.Button1Click(Sender: TObject);
var
SQLString : string;
BlobStream : TStream;
FileStream : TFileStream;
arr: OleVariant;
begin
SQLString := 'select * from 模板信息表 where ID = 4';
with ADOQuery1 do
begin
Close;
SQL.Clear;
SQL.Add(SQLString);
ConnectionString := ConnString;
Open;
BlobStream := CreateBlobStream(FieldByName('模板内容'), bmRead);
if BlobStream.Size = 0 then
begin
BlobStream.Free;
Exit;
end;
arr := StreamToVariant( BlobStream );
BlobStream.Free;
end;
NsoControl1.OpenDocumentWithStream(arr,2);
end;
调用SaveToStream
procedure TForm1.Button2Click(Sender: TObject);
var
arr: OleVariant;
BlobStream: TStream;
fileStream : TStream;
SQLString :string;
memo : TMemoryStream;
i : Integer;
begin
SQLString := 'select * from 模板信息表 where ID = 4';
NsoControl1.SaveToStream(arr);
memo := OleVariantToMemoryStream(arr);
//VariantToStream(arr,fileStream);
i := memo.Size;
if memo.Size = 0 then
exit;
with ADOQuery1 do
begin
Close;
SQL.Clear;
SQL.Add(SQLString);
ConnectionString := ConnString;
Open;
if FieldByName('模板内容').IsBlob then
begin
Edit;
BlobStream := CreateBlobStream(FieldByName('模板内容'), bmWrite);
{Seek to the Begginning of the stream}
BlobStream.Seek(0, soFromBeginning);
memo.Position := 0;
BlobStream.CopyFrom(memo, memo.Size);
{Free the streams}
BlobStream.Free;
memo.Free;
{Post the record}
Post;
end;
end;
end;
function TForm1.OleVariantToMemoryStream(OV: OleVariant): TMemoryStream;
var
Data: PByteArray;
Size: integer;
begin
Result := TMemoryStream.Create;
try
Size := VarArrayHighBound (OV, 1) - VarArrayLowBound(OV, 1) + 1;
if Size = 0 then
begin
if Result <> nil then Result.Free;
Result := nil;
exit;
end;
Data := VarArrayLock(OV);
try
Result.Position := 0;
Result.WriteBuffer(Data^, Size);
finally
VarArrayUnlock(OV);
end;
except
VarArrayUnlock(OV);
Result.Free;
Result := nil;
end;
end;
function TForm1.StreamToVariant(Stream: TStream): OleVariant;
var
p: Pointer;
begin
Result := VarArrayCreate([0, Stream.Size - 1], varByte);
p := VarArrayLock(Result);
try
Stream.Position := 0;
Stream.Read(p^, Stream.Size);
finally
VarArrayUnlock(Result);
end;
end;
调用OpenDocumentWithStream
procedure TForm1.Button1Click(Sender: TObject);
var
SQLString : string;
BlobStream : TStream;
FileStream : TFileStream;
arr: OleVariant;
begin
SQLString := 'select * from 模板信息表 where ID = 4';
with ADOQuery1 do
begin
Close;
SQL.Clear;
SQL.Add(SQLString);
ConnectionString := ConnString;
Open;
BlobStream := CreateBlobStream(FieldByName('模板内容'), bmRead);
if BlobStream.Size = 0 then
begin
BlobStream.Free;
Exit;
end;
arr := StreamToVariant( BlobStream );
BlobStream.Free;
end;
NsoControl1.OpenDocumentWithStream(arr,2);
end;
调用SaveToStream
procedure TForm1.Button2Click(Sender: TObject);
var
arr: OleVariant;
BlobStream: TStream;
fileStream : TStream;
SQLString :string;
memo : TMemoryStream;
i : Integer;
begin
SQLString := 'select * from 模板信息表 where ID = 4';
NsoControl1.SaveToStream(arr);
memo := OleVariantToMemoryStream(arr);
//VariantToStream(arr,fileStream);
i := memo.Size;
if memo.Size = 0 then
exit;
with ADOQuery1 do
begin
Close;
SQL.Clear;
SQL.Add(SQLString);
ConnectionString := ConnString;
Open;
if FieldByName('模板内容').IsBlob then
begin
Edit;
BlobStream := CreateBlobStream(FieldByName('模板内容'), bmWrite);
{Seek to the Begginning of the stream}
BlobStream.Seek(0, soFromBeginning);
memo.Position := 0;
BlobStream.CopyFrom(memo, memo.Size);
{Free the streams}
BlobStream.Free;
memo.Free;
{Post the record}
Post;
end;
end;
end;
- delphi中处理OleVariant和TStream
- delphi中操作oleVariant、variant和stream
- Delphi TStream文件处理 3
- Delphi中Variant/OleVariant转换为接口
- 在Delphi中使用TStream读写数据
- Delphi中TStream 有 Seek() 方法
- Delphi OleVariant 内存泄露
- Delphi 2009 中 TStrings 与 TStream 的增强
- Delphi Tstream & TIdUDPServer
- Delphi TStream 详细介绍
- Delphi TStream 详细介绍
- Delphi 流 Tstream 2
- Delphi TStream 详细介绍
- Delphi OleVariant 类型的用法
- delphi 流 TStream 属性 方法
- 如何在Delphi中使用Microdraw图形控件的OLEVariant类型数据
- 如何在Delphi中使用Microdraw图形控件的OLEVariant类型数据
- Delphi 流文件操作TStream类
- NTDDI_VERSION,_WIN32_WINNT,WINVER,_WIN32_IE定义宏的含义
- Tomcat7.0 在Windows7下的安装
- CPU的大端模式和小端模式
- 关于释放指针引发的一个错误
- Shell脚本编程详解
- delphi中处理OleVariant和TStream
- 产品经理的知识结构是什么
- 虚拟机下的ubuntu用tftp将文件下载到目标板
- oracle查看用户权限
- Delphi中如何使用 MSXMLDOM
- android 显示时间格式24小时或12小时
- 图片上传压缩处理
- 经常对以下项目的梳理
- qt学习笔记(一)之Hello world