Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to use TClientDataset as memory dataset Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
09-Jun-03
Category
DB-General
Language
Delphi 3.x
Views
206
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Mike Shkolnik 

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: 

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

5   for i := 1 to 100 do
6   begin
7     table.Append;
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);
12    table.Post;
13  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: 

14  table.SaveToFile('c:\mem.cds');
15  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.

			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC