FireDAC FDMemTable is an in-memory dataset component that allows the storage of data in table format in local memory. It does not require any database connection so it is very handy on many occasions.
In order to use it, just the fields have to be defined and ready for actions like add, edit, delete records. It can be used for storing data from external files like csv for temporary manipulations.
A. Create fields in memory table
Var
FDMemTable1 : TFDMemTable;
begin;
with FDMemTable1.FieldDefs do
begin
Add(‘EmpID’, ftInteger, 0, True);
Add(‘EmpName’, ftString, 50, False);
end;
FDMemTable1.CreateDataset;
FDMemTable1.Open;
end;
B. Add/Edit data in memory table
with FDMemTable1 do
begin
Append;
//Edit;
Fields[0].AsInteger := 1;
Fields[1].AsString := ‘Surendra’;
Post;
end;
C. Deleting data from memory table
FDMemTable1.Delete;
D. Delete all records from memory table
FDMemTable1.EmptyDataset;
E. Sorting Data
Use Index for sorting. So we can use property Indexes, IndexDefs or IndexFieldNames.
FDMemTable1.IndexFieldNames := ‘AcctID ASC’;
Or
FDMemTable1.IndexDefs.Add(‘Index1’, ‘AcctID’, [ixPrimary]);
FDMemTable1.IndexName := ‘Index1’;
Or
F. Find Data
Just like in other dataset use Locate, Lookup and Find methods to find a specific record.
Locate
FDMemTable1.Locate(‘AcctID’, 1, []);
Or
FDMemTable1.Locate(‘Name’, ’Surendra’, [loCaseInsensitive]);
Lookup
var
sName: string;
sName := FDMemTable1.Lookup(‘AcctID’, 1, ‘Name’);
FindKey
Findkey will work if only any Index is applied
FDMemTable1.FindKey([1]);
FDMemTable1.FindFirst;
FDMemTable1.FindLast;
FDMemTable1.FindNext;
FDMemTable1.FindPrev;
G. Filtering Data
FDMemTable1.Filtered := False;
FDMemTable1.Filter := ‘AcctID=1’;
FDMemTable1.Filtered := True;
H. Copy data from one FDMemTable to another
The most simple way to copy the structure and data from a TdataSet or a TFDMemTable to another TFDMemTable is to use the CopyDataSet method:
FDMemTable1.CopyDataSet(SourceFDMemTable2, [coStructure, coRestart, coAppend]);
FDMemTable1.CopyDataSet(SourceDataset2, [coStructure, coRestart, coAppend]);
Data property
// UniDirectional must be false
FDQuery1.FetchOptions.Undirectional := False;
FDQuery1.Sql.Text := ‘select * from Account’;
FDQuery1.Open;
FDMemTable1.Data := FDQuery1.Data;
FDMemTable1.First;
while not FDMemTable1.Eof do
begin
FDMemTable1.Edit;
// other codes
FDMemTable1.Post;
FDMemTable1.Next;
end;
I. Difference between CopyDataset and Data property
- CopyDataSet can copy from a non-FireDAC dataset but Data property allows you to copy data only from a FireDAC dataset.
- CopyDataSet works through TDataSet firing appropriate events but Data property works through no events are fired.
- CopyDataSet copies only current field values but Data property copies all record field versions and preserves the row state (inserted, deleted, updated, or unchanged).
Assigning values to Data is much faster than CopyDataSet.
Optimize the performance of TFDMemTable
Set the following properties LogChanges, FetchOptions, ResourceOptions, and UpdateOptions, DisableControls.
FDMemTable1.LogChanges := False;
FDMemTable1.ResourceOptions.SilentMode := True;
FDMemTable1.UpdateOptions.LockMode := lmNone;
FDMemTable1.UpdateOptions.LockPoint := lpDeferred;
FDMemTable1.UpdateOptions.FetchGeneratorsPoint := gpImmediate;
FDMemTable1.DisableControls;
FDMemTable1.BeginBatch;
try
for i := 1 to 100 do begin
FDMemTable1.Append;
//other codes
FDMemTable1.Post;
end;
finally
FDMemTable1.EndBatch;
FDMemTable1.EnableControls;
end;