Delphi-Kylix

Using the Client Dataset in Two-Tiered Client/Server Applications

Client-server based applications have a number of advantages over file-based applications. To make such an approach work, however, the client dataset requires a provider component. This article will introduce you to the dataset provider component.

A two-tiered client-server program is divided into two parts, a server and a client which interact with each other over a network. A data storage mechanism is implemented in the server, which allows the client to connect to itself and transact data. A server normally doesn't interact with the end user; it receives requests from the client application, processes them and sends a reply to the client application.

A client, on the other hand, interacts directly with the end user, usually through a graphical interface. It acts as an intermediary between the user and the server.

The two-tiered architecture is based on pretty simple logic, and for that reason it is best suited to performing simple tasks in environments with a small number of users. But even then, if the number of transactions is slightly larger, the network may be overloaded, thus degrading the performance. As I have said earlier, the client dataset component can be used to cache data locally and commit all changes to the server in a single transaction. Using the client dataset in a two-tiered application can help drastically reduce network traffic and enhance performance. So let us take a look at how the client dataset can be used in a two-tier database application.

A distinctive feature of the client dataset is that by itself, it cannot connect to a database source and requires a TDataset Provider class to interact with the data source. The TClient Dataset component points to the dataset provider through its Provider Name property. You can link the client dataset to its dataset provider during design time by selecting the name of the instance of the TDataset Provider in the Object Inspector and during runtime, it can be specified in a single statement.

CDS11.ProviderName:=DataSetProvider1;

The dataset provider in its turn needs to point to another dataset which is linked to the database. This can be done by specifying the dataset's name in the "dataset" property of the dataset provider. When the client dataset is opened it sends a request to the dataset provider which opens the dataset pointing to the external data source, fetches data, converts it to data packets and closes the dataset. It then passes these data packets to the client dataset, which decodes them and loads them into memory, keeping the original structure intact. Another method which you can use to specify the provider to the client dataset is the SetProvider method.

CDS11.SetProvider(DataSetProvider1);

This method can be called to link the client dataset to the dataset provider when the latter resides in the same application but has a different owner. In all other instances the Provider Name property should be used to associate the client dataset with the dataset provider.

In fact, the SetProvider method cannot be used when the provider resides in a remote module; further, the provider you intend to associate with the client dataset has to be a descendant of the TCustomProvider class or else an exception is raised. If you really have to use this method, do remember that this method associates the provider with the client dataset as long as it is open and the linkage is lost when the dataset is closed. So every time you open the client dataset you have to assign the provider using the same method. In contrast to using this method, assigning a provider to a client dataset using its ProviderName property is a much simpler and effective way.

The client dataset allows you to retrieve specific records from its internal and external providers by using its CommandText property. This property can be assigned an SQL statement, table name or stored procedure. When the dataset is opened or the Execute method is called and the CommandText property is not blank, the value held by it is sent to the server through the provider and the result set is sent back to the client dataset.

Do remember that the dataset provider's Option property should have poAllowCommandText set to true to send the CommandText value to the server. It should also be noted that any data held by the client dataset after it has been associated to a data source is replaced with the data retrieved by the execution of the CommandText.