zoukankan      html  css  js  c++  java
  • AdoDataSet.RecordSet的串行化和还原

    //数据集串行化

    function RecordsetToXML(const Recordset: _Recordset): string;
    var
      RS: Variant;
      Stream: TStringStream;
    begin
      Result := '';
      if Recordset = nil then Exit;

      Stream := TStringStream.Create('');
      try
        RS := Recordset;
        // adPersistADTG保存为二进制文件  adPersistXML保存为xml。二进制的传输效率要高于XML
        RS.Save(TStreamAdapter.Create(stream) as IUnknown, adPersistADTG);
        Stream.Position := 0;
        Result := Stream.DataString;
      finally
        Stream.Free;
      end;
    end;

    StrXML := RecordsetToXML(FAdoQuery.Recordset);

    //压缩数据,注意压缩比率不要选择最高
          IsCompress := ECLCompressAndEncryptString(StrXML, OutStr, passWord, CompressionLevel, false);
          if IsCompress then
          begin

    //格式化为16进制字符串
            CompressStr := StrtoFormat(pChar(OutStr), Length(OutStr), fmtHEX);

    //告诉接收方将要发送数据的长度
            AThread.Connection.WriteInteger(Length(CompressStr));
            Stream := TStringStream.Create(CompressStr);

    //发送数据
            AThread.Connection.WriteStream(Stream, True);
            Stream.Free;

     

    //还原数据集

    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;

     

    procedure TTCPAdoClient.Open(Value : String);
    var HexStr, DecStr: String;      
      Stream : TStringStream;  Lenbuf : Integer;
    begin
      Value := Uppercase(Value);
      FIdTCPClient.WriteLn('SELECTSQL: ' + Value);
      FCommand := Value;
      Application.ProcessMessages;
      if not FIdTCPClient.Connected then
        exit;
      LenBuf := FIdTCPClient.ReadInteger;
      Stream := TStringStream.Create('');
      try

    //读取数据
        FIdTCPClient.ReadStream(Stream, LenBuf, False);

        HexStr := FormatToStr(pChar(Stream.DataString), Length(Stream.DataString), fmtHEX);

    //解压缩数据
        ECLDecompressAndDecryptString(HexStr, DecStr, passWord);
        Application.ProcessMessages;
    //还原数据集
        FRecordSet :=  RecordsetFromXML(DecStr);
      finally
        Stream.Free;
      end;
      RecordSet := FRecordSet;
    end;

  • 相关阅读:
    MVC 下载相关
    中缀、前缀和后缀表达式
    什么是堆栈?
    为什么和其他语言相比C语言是快速的语言
    什么是回溯法?
    产生n bit所有可能的序列
    讨论汉诺塔之谜
    递归和内存分配(可视化)
    关于递归
    随机数产生函数的范围转换
  • 原文地址:https://www.cnblogs.com/hnxxcxg/p/2940650.html
Copyright © 2011-2022 走看看