同时支持DELPHI和LAZARUS的二进制序列
同时支持DELPHI和LAZARUS的二进制序列
DELPHI演示
unit Unit1; /// <author>cxg 2022-4-14</author> /// 数据集助手类 演示 /// 支持delphi和lazarus interface uses dataset.serialize.help, keyValue.serialize, Variants, system.JSON, DB, Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Data.Win.ADODB, FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Param, FireDAC.Stan.Error, FireDAC.DatS, FireDAC.Phys.Intf, FireDAC.DApt.Intf, FireDAC.Comp.DataSet, FireDAC.Comp.Client, FireDAC.Stan.StorageBin, Datasnap.DBClient, Vcl.Grids, Vcl.DBGrids, FireDAC.Stan.Async, FireDAC.DApt, FireDAC.UI.Intf, FireDAC.Stan.Def, FireDAC.Stan.Pool, FireDAC.Phys, FireDAC.Phys.MSSQL, FireDAC.Phys.MSSQLDef, FireDAC.VCLUI.Wait, FireDAC.Phys.ODBCBase, MemDS, DBAccess, Uni; type { TForm1 } TForm1 = class(TForm) Button1: TButton; Button2: TButton; Memo1: TMemo; DBGrid1: TDBGrid; DataSource1: TDataSource; FDMemTable1: TFDMemTable; FDQuery1: TFDQuery; FDConnection1: TFDConnection; ClientDataSet1: TClientDataSet; DBGrid2: TDBGrid; DataSource2: TDataSource; Button3: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); private public end; var Form1: TForm1; implementation {$R *.dfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject); begin //序列 var s: TSerialize := TSerialize.Create; FDQuery1.Close; FDQuery1.SQL.Clear; FDQuery1.SQL.Text:='select * from tgoods'; FDQuery1.Open; var ms: TStream := FDQuery1.toStream; s.asstream['ds1'] :=ms; //序列数据集一 ms.Free; FDQuery1.Close; FDQuery1.SQL.Clear; FDQuery1.SQL.Text:='select top 10000 * from tunit'; FDQuery1.Open; ms := FDQuery1.toStream; s.asstream['ds2'] := ms; //序列数据集二 ms.Free; var bs: TBytes := s.marshal2; s.Free; //网络传输 //还原 var s2: TSerialize := TSerialize.Create; s2.unMarshal(bs); ClientDataSet1.fromBytes(s2.asBytes['ds1']); FDMemTable1.fromBytes(s2.asBytes['ds2'] ) ; s2.Free; end; procedure TForm1.Button2Click(Sender: TObject); //同firedac二进制序列较速 begin FDQuery1.Close; FDQuery1.SQL.Clear; FDQuery1.SQL.Text:='select top 40000 * from tunit'; FDQuery1.Open; var b2: Cardinal := TThread.GetTickCount; var ms2:TMemoryStream:=TMemoryStream.Create; FDQuery1.SaveToStream(ms2, sfBinary); ms2.Position:=0; FDMemTable1.DisableControls; FDMemTable1.LoadFromStream(ms2, sfBinary); FDMemTable1.EnableControls; var e2: Cardinal := TThread.GetTickCount; Memo1.Lines.Add('firedac序列耗时: '+(e2-b2).ToString+'ms'); Memo1.Lines.Add('数据序列后大小:'+ ms2.Size.ToString); ms2.Free; var b: Cardinal := TThread.GetTickCount; var ms: TStream:= fdquery1.tostream; // ClientDataSet1.fromStream(bs); // 719ms FDMemTable1.fromstream(ms); //125ms var e: Cardinal := TThread.GetTickCount; Memo1.Lines.Add('我的序列耗时: '+(e-b).ToString+'ms'); Memo1.Lines.Add('数据序列后大小:'+ms.Size.ToString); ms.Free; end; procedure TForm1.Button3Click(Sender: TObject); begin var S: TSerialize := TSerialize.Create; s.Free; end; end.
LAZARUS演示
unit Unit1; {$mode objfpc}{$H+} interface uses keyValue.serialize, dataset.serialize.help, BufDataset, DB, Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, DBGrids, Uni, SQLServerUniProvider; type { TForm1 } TForm1 = class(TForm) BufDataset1: TBufDataset; BufDataset2: TBufDataset; Button2: TButton; DataSource1: TDataSource; DataSource2: TDataSource; DBGrid1: TDBGrid; DBGrid2: TDBGrid; Memo1: TMemo; SQLServerUniProvider1: TSQLServerUniProvider; UniConnection1: TUniConnection; UniQuery1: TUniQuery; UniQuery2: TUniQuery; procedure Button2Click(Sender: TObject); private public end; var Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button2Click(Sender: TObject); //键值对序列data var s, s2: tserialize; bs: tbytes; ms: tstream; i: integer; str: rawbytestring; Data: tbytes; begin //序列 s := TSerialize.Create; uniquery1.Close; uniquery1.SQL.Clear; uniquery1.SQL.Text := 'select * from tgoods'; uniquery1.Open; ms := uniquery1.toStream; s.asstream['ds1'] := ms; //序列数据集一 ms.Free; uniquery1.Close; uniquery1.SQL.Clear; uniquery1.SQL.Text := 'select top 40000 * from tunit'; uniquery1.Open; ms := uniquery1.toStream; s.asstream['ds2'] := ms; //序列数据集二 ms.Free; bs := s.marshal2; s.Free; //网络传输 //还原 s2 := TSerialize.Create; s2.unMarshal(bs); bufdataset1.fromBytes(s2.asBytes['ds1']); bufdataset2.fromBytes(s2.asBytes['ds2']); s2.Free; end; end.
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/16336560.html