The TClientDataSet Component

The TClientDataSet component (Data Access category) is a perfect choice for desktop database applications because of its following qualities:

  • It keeps table data in memory, which makes all operations extremely fast.

  • It can save data in CDS (binary files, smaller than XML) and XML formats.

  • You only have to distribute the MIDAS.DLL dynamic link library (370 KB) with your application.

  • You can also link the entire component into the executable file by including the MidasLib unit in the application's uses list (if you do this, the size of the executable will increase by 252 KB, but you won't need to distribute the MIDAS.DLL dynamic link library with your application).

To see just how easy it is to create a simple database application using the TClientDataSet component, we'll now create a simple application that will display data from the sample image from book biolife.xml file on the form.

To create this application, you should first drop a TClientDataSet and a TDataSource component from the Data Access category on the Designer Surface. You need the TClientDataSet component to access the XML file and the TDataSource component to display database data in data-aware controls on the form.

To display data from the image from book biolife.xml file on the form, you need to assign the image from book biolife.xml file to the FileName property of the TClientDataSet component. To select the image from book biolife.xml file, click the (…) button next to the FileName property in the Object Inspector to display the standard Open dialog. The image from book biolife.xml (and other sample databases) is, by default, stored in the X:\Pro- gram Files\Common Files\Borland Shared\Data directory.

Before adding data-aware controls that will display database data on the form, you need to tell the TDataSource component from which database to read the data required by the data-aware controls. To do this, assign the TClientDataSet component to the DataSet property of the TDataSource component.

Now drop TDBGrid, TDBImage, and TDBNavigator components from the Data Controls category on the Designer Surface, as shown in Figure 19-2.

image from book
Figure 19-2: Several data-aware controls

The TDBGrid is used to display all records from the TClientDataSet, the TDBNavigator is used to enable the user to browse the database, and the TDBImage is used here to preview the images stored in the Graphic field of the image from book biolife.xml database.

To enable these components to use data from the TClientDataSet component, you need to assign the TDataSource component to the DataSource property of all three components. To enable the TDBImage component to display images from the Graphic field, you need to assign the appropriate field to its DataField property, as shown below.

image from book
Figure 19-3: Selecting one of the available fields

Now that you've connected all components, you can set the Active property of the TClientDataSet component to True to see if you've done everything properly. If you have, the TDBGrid and TDBImage components should display data from the image from book biolife.xml database.

image from book
Figure 19-4: Viewing database data at design time

When you use TClientDataSet to access a database, you should not set the Active property to True at design time because the entire database will be linked into the executable file, which would in this case increase the executable by 1 MB. To display the data at run time, you need to open the database by either setting the TClientDataSet's Active property to True or calling its Open method:

procedure TForm1.FormCreate(Sender: TObject); begin   ClientDataSet1.Active := True;   { or }   { ClientDataSet1.Open; } end;

Inside Delphi 2006
Inside Delphi 2006 (Wordware Delphi Developers Library)
ISBN: 1598220039
EAN: 2147483647
Year: 2004
Pages: 212
Authors: Ivan Hladni

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: