Blob参数在多层中如何传递?

ADO+SQL Server 2K+Delphi 5+miads做的三层结构.
中间层RDM自定了一个接口,有一个参数为olevariant类型,把数据库中的image类型的数据
传给Client,想通TMemoryStream把数据读到olevariant中,失败.
问题1:如何把一个TMemoryStream中的数据读到一个olevariant型变量中?
问题2:如上面描述,有否其他方法实现?如何做?


问题1:
  fcontent:olevariant;
  p:pointer;
  stream:tmemorystream;
begin
     ......
        fcontent:=VarArrayCreate([0, Stream.Size - 1], varByte);
        p := VarArrayLock(fcontent);
        try
        Stream.Position := 0;
        Stream.Read(p^, Stream.Size);
        finally
        VarArrayUnlock(fcontent);
        end;
        ....
  客户端
   ....
问题2:image类型的数据据我所知只有有这一种方法、
我可是现给你做出来的噢。:) 


不会吧。我就是这样试验的,没错。
我把原码给你,你参考一下。
1。应用服务器:
procedure Tywh.SqlTest(var filename: WideString; out fcontent: OleVariant);
var str:string;
    Stream:TMemoryStream;
    P:pointer;
begin
  Str:='SELECT filename, fcontent FROM filetable';
  if query_tmp.Active then query_tmp.Close;
  query_tmp.SQL.Clear;
  query_tmp.SQL.Add(str);
  try
    query_tmp.Open;
    if not query_tmp.Eof then
    begin
      while not query_tmp.Eof do
      begin
        if not query_tmp.FieldByName('fcontent').IsNull then
        begin
        //stream转换为OleVariant
        stream:=TMemoryStream.Create;
        FileName:=query_tmp.Fields[0].AsString;
        TBlobField(query_tmp.FieldByName('fcontent')).SaveToStream(stream);
        fcontent:=VarArrayCreate([0, Stream.Size - 1], varByte);
        p := VarArrayLock(fcontent);
        try
        Stream.Position := 0;
        Stream.Read(p^, Stream.Size);
        finally
        VarArrayUnlock(fcontent);
        end;
        break;
        end
        else query_tmp.Next;
      end;
    end;
  except on e:exception do
    FileName:=e.Message;
  end;
end;
2.客户端:
procedure TForm1.Button1Click(Sender: TObject);
var filename:string;
    fcontent:OleVariant;
    fstream:tmemorystream;
    p : Pointer;
    tmpf:textfile;
    i:integer;
    j:tjpegimage;
begin
   if not SocketConnection1.Connected then SocketConnection1.Open;
   try
   SocketConnection1.AppServer.Sqltest(filename,fcontent);
   //OleVariant转换为stream,并把值传递给stream
   fstream:=tmemorystream.Create;
   //j:=tjpegimage.Create;
   fStream.Position := 0;
   i:=sizeof(fcontent);
   fstream.Size:= VarArrayHighBound(fcontent,1)-VarArrayLowBound(fcontent,1)+1;
   p := VarArrayLock(fcontent);
   fStream.Write (p^, fStream.Size);
   VarArrayUnlock (fcontent);
   fstream.LoadFromStream(fStream);
   fstream.SaveToFile('e:\kkk.txt');
   //j.LoadFromStream(fstream);
   //image.Picture.assign(j);
   except on e:exception do
   showmessage(e.Message);
   end;
end;
这回应该不会再有错了吧 

你可能感兴趣的:(blob)