码迷,mamicode.com
首页 > 其他好文 > 详细

序列化ADODataSet, ADOQuery

时间:2019-11-21 17:18:33      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:tst   tool   UNC   com   word   mst   rar   poi   persist   

服务端直接将TADOQuery查询出来的数据直接转换成TStream自己进行传递,到客户端用一个TADOQuery对象还原流就可以了。

方法一

是将recordset保存成流接口->转成OleVariant ->写入流

 

class function TADOTools.saveToStream2(

  pvDataSet: TCustomADODataSet): TMemoryStream;
var
   AStream:_Stream;
   V:OLEVariant;
   P:Pointer;
begin
   AStream:=CoStream.Create;
   OLEVariant(pvDataSet.Recordset).Save(AStream, adPersistADTG);
   AStream.Position:=0;
   V:=AStream.Read(AStream.Size);
   result:=TMemoryStream.Create;
   try
     P:=VarArrayLock(V);
     try
       result.Size:=VarArrayHighBound(V,1)+1;
       Move(P^,result.Memory^, result.Size);
     finally
       VarArrayUnLock(V);
     end;
   except
     result.Free();
     result := nil;
     raise;
   end;
end;
 
class procedure TADOTools.loadFromStream2(pvDataSet: TCustomADODataSet;
  pvStream: TMemoryStream);
var
   V:OLEVariant;
   AR:_Recordset;
   AStream:_Stream;
   P:Pointer;
begin
   pvStream.Position:=0;
   OLEVariant(pvDataSet.Recordset).Open(TStreamAdapter.Create(pvStream) as IUnknown, adPersistADTG);
 
 
   AR.Open(AStream, EmptyParam,adOpenUnspecified, adLockUnspecified, -1);
   pvDataSet.Recordset:=ADOInt._Recordset(AR);
 
 
   V:=VarArrayCreate([0,pvStream.Size-1], varByte);
   P:=VarArrayLock(V);
   try
     Move(pvStream.Memory^, P^, pvStream.Size);
   finally
     VarArrayUnLock(V);
   end;
 
   AStream:=CoStream.Create;
   AStream.Open(EmptyParam,adModeUnknown,adOpenStreamUnspecified,‘‘,‘‘);
   AStream.Type_:=adTypeBinary;
   AStream.Write(V);
 
   AR:=_Recordset(CoRecordset.Create);
   AStream.Position:=0;
   AR.Open(AStream,EmptyParam,adOpenUnspecified, adLockUnspecified, -1);
   pvDataSet.Recordset:=ADOInt._Recordset(AR);
 
end;
 
方法二
class procedure TADOTools.saveToStream(pvDataSet: TCustomADODataSet; pvStream:TStream);
begin
   OLEVariant(pvDataSet.Recordset).Save(TStreamAdapter.Create(pvStream) as IUnknown,
      adPersistADTG);    //adPersistXML
end;
 
class procedure TADOTools.loadFromStream(pvDataSet: TCustomADODataSet;
  pvStream: TStream);
var
   AR:_Recordset;
begin
   AR:=_Recordset(CoRecordset.Create);
   pvStream.Position:=0;
   AR.Open(TStreamAdapter.Create(pvStream) as IUnknown, EmptyParam,adOpenUnspecified, adLockUnspecified, -1);
   pvDataSet.Recordset:=ADOInt._Recordset(AR);
end;

序列化ADODataSet, ADOQuery

标签:tst   tool   UNC   com   word   mst   rar   poi   persist   

原文地址:https://www.cnblogs.com/zyb2016/p/11906305.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!