Save And Load VarChar(Max) As Blob or string From Virtualtable

Discussion of open issues, suggestions and bugs regarding Virtual Data Access Components for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
Javansoft
Posts: 5
Joined: Fri 25 Jun 2021 11:07

Save And Load VarChar(Max) As Blob or string From Virtualtable

Post by Javansoft » Fri 25 Jun 2021 11:18

Hi every body
I looking for this topic about 3 hour , and I collect what I found for next refer and share it with you
I hope you like it
1- we have a virtual table on screen and fill it
2- to save in a stream we can use

Code: Select all

  
  Var 
  NN,K  :TMemoryStream;


procedure TForm1.Button1Click(Sender: TObject);
begin
   NN:=TMemoryStream.Create;
   VirtualTable1.SaveToStream(NN,True,True);
3- we like to save it to MsSql database

Code: Select all

begin
   MSTable1.Insert;
   TBlobField(MSTable1.FieldByName('a')).LoadFromStream(NN);
   MSTable1.Post;
4- We like to save it as a string

Code: Select all


function MemoryStreamToString(M: TMemoryStream): string;
begin
  SetString(Result, PChar(M.Memory), M.Size div SizeOf(Char));
end;

var
   S : TStringList;
begin
   S:=TStringList.Create;
   S.Add('');
   S[0]:=MemoryStreamToString(NN);
   MSTable1.Insert;
   MSTable1.FieldByName('a').AsString:=S[0];
   MSTable1.Post;
5- get data from table and post load into another virtual table

Code: Select all

   K:=TMemoryStream.Create;
   TBlobField(MSTable1.FieldByName('a')).SaveToStream(K);
   VirtualTable2.LoadFromStream(K,True,False);
   VirtualTable2.Active:=True;

Javansoft
Posts: 5
Joined: Fri 25 Jun 2021 11:07

Re: Save And Load VarChar(Max) As Blob or string From Virtualtable

Post by Javansoft » Tue 29 Jun 2021 17:31

Oh I forget
Dont use Ftstring
Just use FtWideString

with use string may you have this message

Code: Select all

"raised exception class EReadError with message 'Stream read error'."

Javansoft
Posts: 5
Joined: Fri 25 Jun 2021 11:07

Re: Save And Load VarChar(Max) As Blob or string From Virtualtable

Post by Javansoft » Wed 30 Jun 2021 11:42

To save and load like a NvarChar(Max)

1- Function that convert TMemoryStream to widestring

Code: Select all

function 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;
2- procedure that let you to load a Memorystream into a widestring

Code: Select all

procedure  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;
3- now save a virtualtable(TVar) into a sqlserver and load it again into another one
(some variables can remove)

Code: Select all

procedure TForm1.Button1Click(Sender: TObject);
var
  A, K: TMemoryStream;
begin
 // save sample table
  A := TMemoryStream.Create;
  TVars.SaveToStream(A);
  MSTable1.Insert;
  MSTable1.FieldByName('aa').AsString := Mem2WS(A);
  MSTable1.Post;
  A.Free;
// load from SQLServer
  A := TMemoryStream.Create;
  WS2Mem(MSTable1.FieldByName('aa').AsString,K) ;
  VirtualTable1.LoadFromStream(K, True, False);
  VirtualTable1.Active := True;
  A.Free;
end;

MaximG
Devart Team
Posts: 1822
Joined: Mon 06 Jul 2015 11:34

Re: Save And Load VarChar(Max) As Blob or string From Virtualtable

Post by MaximG » Fri 13 Aug 2021 09:50

Sorry for the late reply. This example code should work, but if you have difficulties with it, please send us a sample project. For your convenience, please use the e-support form https://www.devart.com/company/contactform.html

Javansoft
Posts: 5
Joined: Fri 25 Jun 2021 11:07

Re: Save And Load VarChar(Max) As Blob or string From Virtualtable

Post by Javansoft » Thu 26 Aug 2021 12:58

Tanx for replay
its work and I use TVirtualtable in my programs very good and useful
I leave my experience for other users

MaximG
Devart Team
Posts: 1822
Joined: Mon 06 Jul 2015 11:34

Re: Save And Load VarChar(Max) As Blob or string From Virtualtable

Post by MaximG » Fri 27 Aug 2021 05:59

We are glad to see the problem resolved. Please don't hesitate to contact us with questions concerning our product usage.

Post Reply