Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
487 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (1)


to use TClientDataset as memory datasetGo to Mike Shkolnik's websiteFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi 3.x (or higher)
Category:
DB-General
Skill Level:
Scoring:
Last Update:
04/03/2003
Search Keys:
delphi delphi3000 article borland vcl code-snippet clientdataset memory dataset in-memory memdata memtable memquery
Times Scored:
8
Visits:
6741
Uploader: Mike Shkolnik
Company: Scalabium Software
Reference: http://www.scalabium.com/faq/dct0150.htm
 
Question/Problem/Abstract:
How to use TClientDataset as memory dataset
Answer:



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 (http://www.scalabium.com/sme)





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
Too bad.
    Abdulaziz Jasser (Apr 29 2006 9:52AM)

The only problem is that you can not use SQL to manipulate the data in it.
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
A. B. Talal
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)