1. Overview
As a rapid development tool under Windows, Delphi can not only develop general Windows applications, but also has powerful database application development functions. Delphi itself provides support for several database drivers such as BDE, ODBC, ADO and InterBase, which can meet the needs of different applications for database program development.
However, when publishing a database program developed with Delphi, in addition to installing the application, you also need to publish the database driver at the same time. This seems a bit top-heavy for some stand-alone applications that only involve single or multiple simple table data storage. Moreover, some applications themselves need to store a large amount of data, but if the results themselves are required to be short and concise, the conventional database development method using Delphi cannot meet the needs.
So, is there any way to solve the above contradiction and develop a "thin" database stand-alone application that can be separated from the huge database driver? Delphi5 provides a TClientDataSet control in the Midas control panel, which can solve this problem well.
2. Key points for using TClientDataSet
The TClientDataSet control inherits from TDataSet, and its data storage file format extension is .cds. It is a control based on file-type data storage and operation. This control encapsulates the interfaces and functions for operating and processing data, and does not rely on the above-mentioned database drivers. It can basically meet the needs of stand-alone "thin" database applications.
1. Introduction to the basic properties and methods of TClientDataSet
1). FieldDefs: field definition list properties
Developers can edit fields by clicking the property editing button in the property editor, or by right-clicking on the control and selecting the "Fields Editor" menu in the pop-up menu. After setting this property, it is actually equivalent to defining the structure of the table; if you want to load the structure and data of an existing data table, you can right-click and select the "Assign Local Data" menu in the pop-up menu. In the dialog box, select the name of the data set control that is connected to the database in the current form (the data set control to be applied must have been placed in the current form and activated).
Notes on use:
For the customized field name table, after editing the property, the control still cannot be opened. You must right-click the control and select the "Create DataSet" menu in the pop-up menu to allow the control to create a data set based on the above-edited field list before it can be activated, opened and used. Otherwise, an error similar to "ClientDataSet1: Missing data PRavider or data packet." will occur (including during runtime, the runtime can call the CreateDataSet method of the control to dynamically define fields and tables).
2). FileNameProperty
Description: The name of the data storage file.
Because this control is a file-based data operation control, you must specify the name of the data file being operated (default extension name .cds) to open and activate the control for data editing.
Example 1: Use this property to open the specified .cds file
var
Path: string;
begin
Path := ExtractFilePath(application.ExeName); //Get the executable file path
CDataSet1.FileName := Path + 'test.cds';
CDataSet1.Open;
end;
3). CreateDataSet method
Description: This method uses the field name table in FieldDefs as a structure to create a data set, which is often used to dynamically define tables.
Example 2: Dynamically create a data set with two fields: name and age.
//Create field name table
CDataSet.FieldDefs.Clear;
with CDataSet.FieldDefs.AddFieldDef do
begin
Name := 'Name';
Size := 10;
DataType := ftString;
end;
with CDataSet.FieldDefs.AddFieldDef do
begin
Name := 'Age';
DataType := ftInteger;
end;
//Dynamicly create data set
CDataSet.CreateDataSet;
//Activate and open the data set
CDataSet.Open;
4). Open method
Description: Opens and activates the data set control for data editing.
a. If the FileName attribute is specified, the control can be opened and activated directly using the Open method, see Example 1.
b. If the FileName attribute is not specified, you can use the method in Example 2 to dynamically create and open the data set and then manipulate the data.
5). LoadFromFile and SaveToFile
Description: Load table structures and data from files and store data to files. This method is similar to the Open New File and Save As functions in Word.
Example 3: Store the data of the dataset into the specified file
CDataSet.SaveToFile('c:/windows/desktop/test.cds');
6).First (to the beginning), Prior (forward), Next (backward), Last (to the end), Edit (edit), CanCel (cancel editing), Post (save), Insert (insert record), Append (Add record), Delete (delete), Refresh (data refresh) and other common methods of data sets
Note: When the FileName attribute is specified, its Post method can store data in the specified file, similar to its SaveToFile method; if the storage file name is not specified, the Post method only stores the data in RAM. Other methods are the same as the general data set control usage, omitted.
7).Filter, Filtered: filtering properties
Description: Used to filter records with specified conditions. The usage is the same as that of general data set controls, which is omitted.
Example 4: Filter records with male gender in the activated and opened data set
CDataSet.Close;
CDataSet.Filter := 'Gender=''' + 'Male' + '''';
CDataSet.Filtered := True;
CDataSet.Open;
2. Notes for application publishing using the TClientDataSet control:
As mentioned before, programs using the TClientDataSet control do not require any database driver when released, which greatly saves the size of the installation file. However, when publishing the program, don't forget to publish midas.dll (257KB) in the Windows system directory together with the application (required for operation), otherwise, the program will still not run normally.
3. Conclusion
By using the TClientDataSet control in Delphi, the application can be completely separated from the database driver, and the simple and easy-to-use characteristics of the conventional data set control can be realized, providing a technical method and means for writing "thin" database applications.
The above program has been tested under Pwindows98 and Delphi5.