Home arrow Delphi-Kylix arrow Page 2 - Using the Client Dataset in File-Based Architecture
DELPHI-KYLIX

Using the Client Dataset in File-Based Architecture


A client dataset can be used as a fully functional file-based dataset in a single-tier application as well as the client part of a multi-tiered application. A file-based application is the simplest form of database application, and it does not require a database server to save data in. Instead, it uses MyBase, the ability of client datasets to save themselves to a file and to load the data from a file.

Author Info:
By: Danish Ahmed
Rating: 5 stars5 stars5 stars5 stars5 stars / 5
December 19, 2007
TABLE OF CONTENTS:
  1. · Using the Client Dataset in File-Based Architecture
  2. · Adding FieldDefs
  3. · Adding IndexDefs
  4. · Loading and Saving Files

print this article
SEARCH DEVARTICLES

Using the Client Dataset in File-Based Architecture - Adding FieldDefs
(Page 2 of 4 )

While taking the above approach you do require a database server even if for a very short time. If you do not want to use a database connection, you can start from scratch by creating a table to work with. But first you need to specify the structure of the table and set its property before creating the dataset. In fact the Create Dataset option in the context menu of the client dataset is not available unless the structure of the dataset has been defined. To specify the structure of the dataset you can first create field definitions.

Field definitions can be created during design time and runtime by using the TFieldDef object. A FieldDef object contains the definition of a field and includes attributes of the field like name, data type and size. Use the FieldDefs property of the client dataset (available in the Object Inspector) to define the structure of the table. You can edit the FieldDefs property from the Object Inspector during the design time and during runtime you can use the Add method or AddFieldDefs routines to define FieldDefs.

To define FieldDefs at design-time bring up the FieldDefs collection editor by clicking the ellipsis button in the FieldDefs property of the client dataset in the Object Inspector. Add a new field by clicking on the "Add New" button. Select the field you just created and change its properties in the Object Inspector. You need to specify the value to the DataType property of the FieldDef instance; also you should specify a name or else the default FieldDefs name will be used.

There are several other properties, some of which do not require you to change the default value assigned to them by Delphi, but you can opt to change them if the situation demands. For instance, you can change the Required property to true if you do not want to allow null values in the particular field.

After you have created the FieldDefs, click on Create Dataset in the context menu. The data will now be stored in memory in the tabular format that you specified. The dataset will take the definitions of the fields from the FieldDefs collection that you modified. The context menu also provides you with the feature to save data to a file. Just click on the Save MyBase file to save in binary format or Save to XML table to save data as XML. More on that later.  

You can also create FieldDefs during runtime by calling the Add method or the AddFieldDefs method of the TFieldDefs class.

ClientDataSet1.FieldDefs.Add(const Name:string;DataType:TFieldType;[Size:Integer=0];[Required:Boolean=False]);

The Add method requires four parameters: Name, DataType, Size and Required. The Name property corresponds to the name you want to assign the field. The DataType parameter contains the TFieldType value.The Size property, as is apparent from the name itself, contains information on the size of the field and the boolean parameter 'required' specifies whether the field can be empty.

ClientDataSet1.FieldDefs.Add('fName',ftString,25,False);

The other method that you can use to define FieldDefs is the AddFieldDef which is a member method of the TFieldDef class.


function AddFieldDef(): TFieldDef;

This method returns a new TFieldDef object which is added to the collection of field definitions. After creating a new FieldDef, assign values to its properties or attributes like Name, DataType and Size to make sure that your table is properly structured.


ClientDataSet1.FieldDefs.AddFieldDef.Name:='fName';


Alternatively you can use a with.. do.. block of statements to assign values to multiple properties of a newly created FieldDef.


with

ClientDataset2.FieldDefs do

begin

with AddFieldDef

do

begin

Name:='fName';

DataType:=ftString;

Size:=255;

end;

with AddFieldDef

do

begin

Name:='lName';

DataType:=ftString;

Size:=255;

end;


with AddFieldDef

do

begin

Name:='phNo';

DataType:=ftInteger;

Required:=True;

end;

with AddFieldDef

do

begin

Name:='Email';

DataType:=ftString;

Size:=255;

end;


end;


blog comments powered by Disqus
DELPHI-KYLIX ARTICLES

- Loading an XML Document into the DOM
- Delphi Wrapper Classes and XML
- Delphi and the DOM
- Delphi and XML
- Internet Access: Client Service
- Finishing the Client for an Internet Access ...
- The Client for an Internet Access Control Ap...
- User Management for an Internet Access Contr...
- Important Procedures for an Internet Access ...
- Server Code for an Internet Access Control A...
- Constructing the Interface for an Internet A...
- Building a Server Application for an Interne...
- Building an Internet Access Control Applicat...
- Client Dataset: Working with Data Packets an...
- Using the Client Dataset in an N-Tiered Appl...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials