Scalabium Software

SMExport/SMImport suites
Knowledge for your independence'.
Home Delphi and C++Builder tips


#150: To use TClientDataset as memory dataset

As you know starting from Delphi 3, Borland included TClientDataset component as a database-independent engine.

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)
- DevExpress MemData
- KbmMemTable
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 form):

table := TClientDataset.Create(Application);

2. you must add the field defintions:

table.FieldDefs.Add('ID', ftInteger, 0, False);
table.FieldDefs.Add('Status', ftString, 10, False);
table.FieldDefs.Add('Created', ftDate, 0, False);
table.FieldDefs.Add('Volume', ftFloat, 0, False);

3. create a dataset with specified structure:

table.CreateDataset

4. open a dataset

table.Open

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:

  for i := 1 to 100 do
  begin
    table.Append;
    table.FieldByName('ID').AsInteger := i;
    table.FieldByName('Status').AsString := 'Code'+IntToStr(i);
    table.FieldByName('Created').AsDateTime := Date();
    table.FieldByName('Volume').AsFloat := Random(10000);
    table.Post;
  end;

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 file:

table.SaveToFile('c:\mem.cds');
table.LoadFromFile('c:\mem.cds');

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 record etc

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. Also you may export it in any file format using SMExport suite


Published: April 3, 2003

See also
 
MAPIMail
Excel Web-stream
Protected Storage Viewer
SMMsg suite
Metafile Convert
Excel Reader (dll)
Paradox to Text converter
Paradox Password Recovery
Paradox ActiveX
SMImport suite
 
 


Contact to webmaster

 

Borland Software Code Gear Scalabium Delphi tips

Copyright© 1998-2024, Scalabium Software. All rights reserved.
webmaster@scalabium.com

SMReport Autogenerated