ado數據集對象流化與對象還原

通過網絡傳輸對象之前,必須要對對象進行串行化(流化)處理,然後發送串行化的流。當接收方根據接收到的流還原對象出來。

function RecordsetToXML(const Recordset: _Recordset): string;

var

  RS: Variant;

  Stream: TStringStream;

begin

  Result := '';

  if Recordset = nil then Exit;

  Stream := TStringStream.Create('');

  try

    RS := Recordset;

    RS.Save(TStreamAdapter.Create(stream) as IUnknown, adPersistADTG);

    Stream.Position := 0;

    Result := Stream.DataString;

  finally

    Stream.Free;

  end;

end;

 

function RecordsetFromXML(const XML: string): _Recordset;

var

  RS: Variant;

  Stream: TStringStream;

begin

  Result := nil;

  if XML = '' then Exit;

  try

    Stream := TStringStream.Create(XML);

    Stream.Position := 0;

    RS := CreateOleObject('ADODB.Recordset');

    RS.Open(TStreamAdapter.Create(Stream) as IUnknown);

    Result := IUnknown(RS) as _Recordset;

  finally

    Stream.Free;

  end;

end;

將對象串行化(流化)處理後,必須調用StrToHex()方法格式化字符串,然後進行加密、發送。。。。。。

 

function TransChar(AChar: Char): Integer;
begin
  
if AChar in ['0'..'9'] then
  Result := Ord(AChar) - Ord('0')
  
else
  Result := 10 + Ord(AChar) - Ord('A');
end;
function StrToHex(AStr: string): string;
var
I ,Len: Integer;
s:char;
begin
  len:=length(AStr);
  Result:='';
  
for i:=1 to len  do
  
begin
    s:=AStr[i];
    Result:=Result +' '+IntToHex(Ord(s),2);
//将字符串转化为16进制字符串,
                                            //并以空格间隔。
  end;
  Delete(Result,1,1);
//删去字符串中第一个空格
end;
function HexToStr(AStr: string): string;
var
I,len : Integer;
CharValue: Word;
Tmp:
string;
s:char;
begin
  Tmp:='';
  len:=length(Astr);
  
for i:=1 to len  do
  
begin
    s:=Astr[i];
    
if s <> ' ' then Tmp:=Tmp+ string(s);
  
end;
  Result := '';
  
For I := 1 to Trunc(Length(Tmp)/2) do
  begin
    Result := Result + ' ';
    CharValue := TransChar(Tmp[2*I-1])*16 + TransChar(Tmp[2*I]);
    
if (charvalue < 32) or (charvalue > 126)  then Result[I] := '.'   //非可见字符填充
    else Result[I] := Char(CharValue);
  
end;
end;

posted @ 2009-10-19 10:55  delphi中间件  阅读(248)  评论(0编辑  收藏  举报