//数据集串行化
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;