Author: Mike Shkolnik
How to use TClientDataset as memory dataset
As you know starting from Delphi 3, Borland included TClientDataset component as a
Generally this component is used for multi-tier environments when you transfer from
server application to client application your data (using TProvider).
But today I want to show how to use standard TClientDataset component for
memory-dataset without any multi-tiered application.
On any archive you may find some third-party memory datasets:
TRxMemoryData from freeware RxLib/JVCL (I must say that only TMemoryTable from RX
depends from BDE. TRxMemoryData don't use BDE)
and a lot of another components
Also some from you use third-party database engines (Apollo/DBISAM/Halcyon/...) in
mode when you don't need a real database. Exists a lot of task when real database
is not required. All what you need is some temporary dataset.
So I want to show how to use TClientDataset in such mode on small sample.
1. you must create a TClientDataset instance. You may do it in design-time (simply
drop a component on form) or in run-time (for example, in OnCreate event of your
table := TClientDataset.Create(Application);
2. you must add the field defintions:
1 table.FieldDefs.Add('ID', ftInteger, 0, False);
2 table.FieldDefs.Add('Status', ftString, 10, False);
3 table.FieldDefs.Add('Created', ftDate, 0, False);
4 table.FieldDefs.Add('Volume', ftFloat, 0, False);
3. create a dataset with specified structure:
4. open a dataset
5. it's all! Now you may add/edit/delete records, change an order (sort) and any
another action that is available for any dataset.
For example, to add random values to records:
5 for i := 1 to 100 do
8 table.FieldByName('ID').AsInteger := i;
9 table.FieldByName('Status').AsString := 'Code' + IntToStr(i);
10 table.FieldByName('Created').AsDateTime := Date();
11 table.FieldByName('Volume').AsFloat := Random(10000);
6. if you want to change an order for records, simply change IndexFieldNames
property. For example, next command will sort your memory dataset by Created field:
table.IndexFieldNames := 'Created';
7. note that TClientDataset also allow to save memory dataset to file and load from
A few file formats are supported - internal cds-format and xml-format
Of course, you may use SMImport suite for save/load too - it will work with such
memory dataset without any problems. So you may expand your application and load
data from MS Excel-spreadsheet or MS Access database, for example.
So in such manner you may transfer your data between applications/computers, update
As example, you may use xml-file instead ini-file and store there any number of
items without limitations on size/value types etc. Just load it to TClientDataset
and navigate thru stored options as thru dataset.
PS: of course, you may also display such memory dataset in DBGrid or print it using any report engine.