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;
Next: Adding IndexDefs >>
More Delphi-Kylix Articles
More By Danish Ahmed