I. Overview
As a rapid development tool under Windows, Delphi is not only developing a general Windows application, but also has a powerful database application development feature. Delphi itself provides support for several database drives for BDE, ODBC, ADO, and InterBase, which can meet the needs of different applications on database programs.
However, when publishing a database program developed with Delphi, in addition to installing an application, it is necessary to simultaneously publish the database driver. This seems a bit of a bit of a lightweight feeling for some stand-alone applications that only involve only single or multiple simple table data stored. Moreover, some applications themselves need to store large amounts of data, but itself requires short-grade, and the method of using the Delphi regular development database cannot meet the needs.
So, is there a way to solve the above contradictions, develop a "thin" database single-machine 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.
Second, TclientDataSet Type Points
The TclientDataSet control is inherited from TDataSet, and its data storage file format extension is .cds, which is based on file type data storage and operation. The control encapsulates the interfaces and functions of data processing, which itself does not rely on the above database drivers, basically meet the needs of the single "thin" database application.
1. Basic attributes and methods for TclientDataSet
1). FieldDefs: Field definition list properties
Developers can edit buttons by clicking on this property editor or on this control, select the "Fields Editor" menu in the pop-up menu for field editing. 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 the existing data table, you can select the "Assign Local Data" menu in the pop-up menu by clicking Right click. The dialog box is selected in the current form that has been connected to the database is available (the current form must be placed in a set of data set controls to be set and activated).
Use Note:
For custom field names, the control is still unable to open. Right-click on the control, select the "CREATE DATASET" menu in the pop-up menu, so that the control is based on the list of fields described above. After the data set is created, it can be activated and used. Otherwise, an error similar to "ClientDataSet1: Missing Data Provider or Data Packet" (including the CREATETASET method for the control can be called in the running period, "thereby dynamically defining the fields and tables).
2). FileName property
Description: The name of the data store file.
Because the control is file-based, the data file name (default extension name .cds) must be specified, turn on and activate the control, and then perform data editing.
Example 1: Use this property to open the specified .cds file
VAR
PATH: STRING;
Begin
PATH: = extractFilePath (Application.exename); / / get the executable path
CDataSet1.FileName: = Path 'Test.cds';
CDataSet1.Open;
END;
3). CreateDataSet method
Note: This method establishes a dataset in the field name table in FieldDefs, which is often used to perform a dynamic definition table.
Example 2: Dynamically create a data set with two fields of name and age. // Create a field name table
CDataSet.fielddefs.clear;
With cdatanet.fielddefs.addfielddef do
Begin
Name: = 'Name';
SIZE: = 10;
DataType: = ftstring;
END;
With cdatanet.fielddefs.addfielddef do
Begin
Name: = 'agn';
DataType: = ftinteger;
END;
// Dynamically create data set
CDataSet.created;
/ / Activate and open the data set
CDataSet.open;
4). Open method
Description: Open and activate the data set control, thereby performing data editing.
a. If the FileName property is specified, the control can be opened and activated directly, see Example 1.
b. If the FileName property is not specified, use Example 2 to dynamically create and open the data set, and then operate the data.
5). LoadFromfile and Savetofile
Description: Load table structure and data from the file and store data to files. This method is similar to that open new files and saving functions in Word.
Example 3: Store data from the data set into the specified file
CDataSet.savetofile ('c: /windows/desktop/test.cds');
6) .first (to the first), prior (forward), next (back), Last, Edit, Cancel (Cancel Editing), POST (Save), Insert (Insert), Append (Add record), DELETE, Refresh (data refresh), etc.
Note: When the filename property is specified, its post method can store the data into the specified file, similar to its SaveTofile method; if the store name is not specified, the POST method only stores data in the RAM. Other methods, the use of general data set controls, slightly.
7) .filter, Filtered: Filter screening properties
Description: Used to filter the record of the designated condition, usage with the general data set control, slightly.
Example 4: Screening gender for male records in data concentrations that have been activated
CDataSet.Close;
CDataSet.filter: = 'Gender =' '' 'Male' '' '';
CDataSet.filtered: = true;
CDataSet.open;
2. Precautions published using the application of the TclientDataSet control:
As mentioned earlier, any database driver does not require any database driver when using the program of the TclientDataSet control, which greatly saves the size of the installation file. However, don't forget to publish the Midas.dll (257kb) of the Windows System Directory when publishing procedures (running must be running), otherwise the program still does not function properly.
Third, conclude
By using the TclientDataSet control in Delphi, the application can be thoroughly disengaged from the database driver, and the simple and easy-to-use feature of the general data set control is also implemented, providing a technical method and means for writing "thin" database applications.
The above procedures are passed under PWindows98 and Delphi5.