mikelittlewood
mikelittlewood

Reputation: 223

In memory persistance of TFDMemTable data

I'm trying to work out whether I can persist the data stored in a TFDMemTable after closing the dataset without having to save it to a file.

I checked the TResourceOptions.Persistent but this will only save to the file name specified in TResourceOptions.PersistentFileName at runtime. You can save data at design time in the dfm if you leave the filename blank, but this isn't useful.

I also looked at the .SaveToStream/LoadFromStream but again that only saves/loads to a file specified in TResourceOptions.PersistentFileName, I was hoping I could just keep it in a local memory stream.

I have the DevExpress components which I know theirs can persist the data, but I'm trying to use the FDAC REST examples which have built in functionality for transferring the tables as JSON.

Am I missing a setting somewhere that will allow me to persist the data, or does anyone have a method to do it?

Upvotes: 1

Views: 1375

Answers (1)

MartynA
MartynA

Reputation: 30715

The following works fine for me:

procedure TForm1.Button5Click(Sender: TObject);
var
  MS : TMemoryStream;
begin
  //  Requires TFDStanStorageBinLink on form/datamodule
  MS := TMemoryStream.Create;
  try
    FDMemTable1.SaveToStream(MS);
    FDMemTable1.Close;
    // sometime later
    MS.Position := 0;
    FDMemTable1.LoadFromStream(MS);
  finally
    MS.Free;
  end;
end;

Upvotes: 1

Related Questions