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.
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 FieldDefsproperty 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 Addmethod 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 FieldDefsname 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 AddFieldDefsmethod of the TFieldDefs class.
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.
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.