Home arrow Delphi-Kylix arrow Page 4 - Using the Client Dataset in File-Based Architecture
DELPHI-KYLIX

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.

Author Info:
By: Danish Ahmed
Rating: 5 stars5 stars5 stars5 stars5 stars / 5
December 19, 2007
TABLE OF CONTENTS:
  1. · Using the Client Dataset in File-Based Architecture
  2. · Adding FieldDefs
  3. · Adding IndexDefs
  4. · Loading and Saving Files

print this article
SEARCH DEVARTICLES

Using the Client Dataset in File-Based Architecture - Loading and Saving Files
(Page 4 of 4 )


Finally, you can load data from a previously saved .cds file or XML document. To load data from a flat file during design time, select the client dataset component and bring up its context menu. It would be a good idea to make sure that the dataset is empty. Choose Clear Data from the Menu Options. Click on "Load from MyBase File" and select the file in the dialog box that pops up.

Call the LoadFromFile method to load data from an existing file to the dataset. You need to be sure that the data has been saved in the client dataset format by a client dataset component using its SaveToFile method.


procedure LoadFromFile(const FileName: string);

The LoadFromFile method requires only one parameter, which is the file that should be used to fetch data from and populate the dataset. If you load from the same file every time then you don't need to call this method; instead you can assign the file to the filename property of the client database so that the data from the file is loaded every time the application starts and is saved at the termination of the application.

You can also load data from an XML document that was not saved by the client dataset by employing a TXMLTransformProvider component to transform an XML document into data packets which can then be read into the client dataset. The client dataset, along with an XML provider, lets you use an XML document as if it were a database server. Using an XML document (with the help of a TXMLTransformProvider component) to load data is the second approach to creating a dataset that does not represent server data. 

Data assigned to the client dataset using the first and the second approach is saved in the form itself so the size of your Form file (.dfm) would be considerably greater. Call the client dataset's SaveToFile method to save data to a local file. The client dataset can save a file in .cds and XML format.To save data to a file you can call the client dataset's member method, SaveToFile.


procedure SaveToFile(const FileName: string; Format: TdataPacketFormat);


In the above method the 'FileName' parameter refers to the name of the file to which you want to save data and with the 'Format' parameter you can specify the format you want the file saved in. This could be binary (dfBinary) or XML (dfXML), or UTF8-based XML (dfXMLUTF8). The file is saved with a .cds extension or a .xml extension depending on what you specify in the method.

Although both parameters in the above method are optional, you need to provide it with a file name in which you wish to save data, as if the dataset's filename property has not already been assigned a value. If a filename has been specified, data is saved in a file bearing that name.

If you omit the second parameter, data is saved in the native format of the file type you chose to save it as. This means that if you assigned a filename with a .xml extension to the filename property of the dataset or specified the first parameter as a filename with .xml extension, the data will be stored in XML  format. For any other extension, data will be saved in binary format. However, if you want to save data as XML but want to save the file with a different extension you can use the Format parameter of the SaveToFile method. 

Do remember that if you have specified a value to the filename property of the client dataset, you need to deploy the file along with the application (along with midas.dll). Also remember that you don't need to call the LoadFromFile and SaveToFile routines, the client dataSet will load data from the file specified in its filename property when it is opened and will save data to the file when it is closed. 

The client dataSet is a non-visual component, which means that neither the data nor the component itself is visible during runtime, but we can design a very simplistic front end interface using data-aware controls to display data held in the dataset. Drop a TDataSource component and set its dataset property to point to the client dataset. Next drop a TDBGrid component and set its data source property to the data source component we just added, namely DBGrid. Set the active property of the client dataset to true. You should now be able to view the data in the DBGrid component. To navigate and edit, add a TDBNavigator and link it to data source component.

We now have a fully functional database application that saves data to a flat file. In the next installment we will look at using the client dataset with a dataset provider in a two-tiered application architecture.

 

 

 

 


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus
DELPHI-KYLIX ARTICLES

- Loading an XML Document into the DOM
- Delphi Wrapper Classes and XML
- Delphi and the DOM
- Delphi and XML
- Internet Access: Client Service
- Finishing the Client for an Internet Access ...
- The Client for an Internet Access Control Ap...
- User Management for an Internet Access Contr...
- Important Procedures for an Internet Access ...
- Server Code for an Internet Access Control A...
- Constructing the Interface for an Internet A...
- Building a Server Application for an Interne...
- Building an Internet Access Control Applicat...
- Client Dataset: Working with Data Packets an...
- Using the Client Dataset in an N-Tiered Appl...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials