序列化ADODataSet, ADOQuery

经过昨天晚上一折腾把做了一个DIOCP直接传递TADOQuery的Demo,在google上找了一系列的资料。

这样服务端可以直接将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;

 

 

今天稍微改造了下变得简单了

直接调用recordset保存成流借用TStreamAdapter

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;

 

你可能感兴趣的:(query)