delphi中处理OleVariant和TStream

因为客户不知道从数据库中流字段到打开文档,编辑,再保存到数据库中流字段这个流程,这个流程涉及到了接口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,String,Stream,Integer,Delphi)