Learning : How to save a Virtual table in SQL Tables
Sometimes we need to store a memory table (Virtual table is the best :) ) in a field of a database table. I present these 2 sub-programs and how to use them as a review.
//--------- Wide string to TStringStream
procedure TDMT.WS2Mem(S: string; Var K : TMemoryStream);
var
StringStream: TStringStream;
begin
StringStream:=TStringStream.Create(S);
K:=TMemoryStream.Create;
K.Position := 0;
K.CopyFrom(StringStream,StringStream.Size);
end;
//--------- TStringStream to WideString
function TDMT.Mem2WS(MemoryStream : TMemoryStream): string;
var
StringStream: TStringStream;
begin
Result:='';
StringStream:= TStringStream.Create('');
try
MemoryStream.Position := 0;
StringStream.CopyFrom(MemoryStream, MemoryStream.Size);
Result:= StringStream.DataString;
Result := Result;
finally
FreeAndNil(StringStream);
end;
end;
Example of using
VarTable:=TVirtualTable.Create(Self);
with VarTable Do
Begin
Close;
if DMT.Mem2WS(InVarStr)<>'' then
LoadFromStream(InVarStr);
Else
Begin
AddField('Fname',ftString,20);
AddField('FVal',ftWideString,20);
End;
Var
VarStr : TMemoryStream;
begin
VarStr:=TMemoryStream.Create;
DMT.WS2Mem(DMT.QAskAnswers.FieldByName('SavedValues').AsString,VarStr );
Try
MemParams.Close;
MS:=TMemoryStream.Create;
WS2Mem(DMT.QAskAnswers.FieldByName('RepParameters').AsString,MS);
if Mem2WS(MS) <> '' then
MemParams.LoadFromStream(MS);
Finally
FreeAndNil(MS);
MemParams.open;
End;