咏南中间件统一的数据序列(还原)类
咏南中间件统一的数据序列(还原)类
1)支持DELPHI7及以上版本(已测试通过),更低DELPHI版本,有待测试,理论上也是能够支持的
2)支持序列/还原的基本类型包括:string, integer, bool, date, time, datetime, float
3)支持扩展的类型包括:buffer, stream, file, bytes, stringlist, oleVariant
4)支持直接序列/还原 数据集对象
5)支持跨平台序列/还原
支持的序列/还原接口:
procedure WriteString(const key, value: string); procedure WriteInteger(const key: string; value: integer); procedure WriteFloat(const key: string; value: Double); procedure WriteBool(const key: string; value: Boolean); procedure WriteDate(const key: string; value: TDateTime); procedure WriteTime(const key: string; value: TDateTime); procedure WriteDateTime(const key: string; value: TDateTime); procedure WriteStream(const key: string; value: TStream); procedure WriteBuf(const key: string; const buf; const len: integer); procedure WriteBytes(const key: string; const bytes: TBytes); procedure WriteStringList(const key: string; const list: TStringList); procedure WriteFile(const key, filename: string); {$IFDEF fd} procedure WriteFDDataSet(const key: string; dataset: TFDAdaptedDataSet); procedure WriteFDDelta(const key: string; dataset: TFDAdaptedDataSet); {$ENDIF} {$IFDEF ole} procedure WriteOleVariant(const key: string; ole: OleVariant); procedure WriteDelta(const key: string; cds: TClientDataSet); procedure WriteParams(const key: string; params: TParams); {$ENDIF} //-------------------------------------------------------------------------- function ReadString(const key: string): string; function ReadInteger(const key: string): Integer; function ReadFloat(const key: string): Double; function ReadBool(const key: string): Boolean; function ReadDate(const key: string): TDateTime; function ReadTime(const key: string): TDateTime; function ReadDateTime(const key: string): TDateTime; procedure ReadStream(const key: string; value: TStream); procedure ReadBuf(const key: string; var buf; const len: integer); procedure ReadBytes(const key: string; var bytes: TBytes); procedure ReadStringList(const key: string; var list: TStringList); procedure ReadFile(const key: string; stream: TStream); {$IFDEF fd} procedure ReadFDDataSet(const key: string; dataset: TFDMemTable); procedure ReadFDDelta(const key: string; stream: TStream); {$ENDIF} {$IFDEF ole} function ReadOleVariant(const key: string): OleVariant; procedure ReadClientDataSet(const key: string; cds: TClientDataSet); procedure ReadParams(const key: string; params: TParams); {$ENDIF}
下面是序列/还原的演示:
procedure TForm1.Button1Click(Sender: TObject); // 基本类型 序列/还原 var serial, serial2: TynSerial; s: string; begin serial := TynSerial.Create; serial.WriteString('str', 'yn'); serial.WriteInteger('int', 888); serial.WriteDateTime('now', now); serial.WriteFloat('float', 12.68); serial.WriteBool('bool', True); s := serial.Text; serial2 := TynSerial.Create; serial2.Text := s; Memo1.Lines.Add(serial2.ReadString('str')); Memo1.Lines.Add(IntToStr(serial2.ReadInteger('int'))); serial.Free; serial2.Free; end; procedure TForm1.Button2Click(Sender: TObject); // 流 序列/还原 var ms: TMemoryStream; serial, serial2: TynSerial; s: string; begin ms := TMemoryStream.Create; ms.LoadFromFile(ExtractFilePath(ParamStr(0)) + '1.bmp'); ms.Position := 0; serial := TynSerial.Create; serial.WriteStream('img', ms); s := serial.Text; serial2 := TynSerial.Create; serial2.Text := s; ms.Clear; serial2.ReadStream('img', ms); ms.Position := 0; Image1.Picture.Bitmap.LoadFromStream(ms); ms.Free; serial.Free; serial2.Free; end; procedure TForm1.Button3Click(Sender: TObject); // buffer 序列/还原 var r, r2: TRec; serial, serial2: TynSerial; s: string; begin r.id := 1; r.name := 'buf'; serial := TynSerial.Create; serial.WriteBuf('buf', r, SizeOf(r)); s := serial.Text; serial2 := TynSerial.Create; serial2.Text := s; serial2.ReadBuf('buf', r2, SizeOf(r2)); Memo1.Lines.Add(IntToStr(r2.id)); Memo1.Lines.Add(r2.name); serial.Free; serial2.Free; end; procedure TForm1.FormCreate(Sender: TObject); begin ADOConnection1.Connected := True; end; procedure TForm1.Button4Click(Sender: TObject); // 数据集 序列/还原 var serial, serial2: TynSerial; s: string; begin ADOQuery1.Close; ADOQuery1.SQL.Clear; ADOQuery1.SQL.Add('select * from tgoods'); serial := TynSerial.Create; serial.WriteOleVariant('goods', DataSetProvider1.Data); ADOQuery1.Close; ADOQuery1.SQL.Clear; ADOQuery1.SQL.Add('select * from tunit'); serial.WriteOleVariant('unit', DataSetProvider1.Data); s := serial.Text; serial2 := TynSerial.Create; serial2.Text := s; serial2.ReadClientDataSet('goods', ClientDataSet1); serial2.ReadClientDataSet('unit', ClientDataSet2); serial.Free; serial2.Free; end;
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/10451505.html