Re: convert database to XML without BDE

Posted by John, DelphiLand Team on March 09, 2009

In Reply to convert database to XML without BDE posted by Phil Birell on March 07, 2009

: How do I convert my Delphi programs from using DBF files to XML files, without the BDE drivers? Recently I read that with ClientDataSet and XML files I don't need the BDE (since Delphi 7?)
: So that my database programs run on my friends' computers, without installing the BDE on the other computers. The databases are small, less than thousand records, would it become much slower?

Unlike the other Delphi dataset components, ClientDataSet holds all its data in memory. This may sound like a limitation, but consider that 10000 records of 1000 bytes records consume only 10MB of memory, a piece of cake for nowadays personal computers. On the upside, a ClientDataSet is very fast, because it operates entirely in RAM, instead of accessing a disk for each operation.

Let's look at a simple project "Contries" located in folder C:\Contries. It reads/writes/displays data from the local DBF file Contries.dbf. The project has only 1 form, and on the form are the following components:

- Table1, connected to the physical file C:\Contries\Contries.dbf;
- DataSource1, connected to Table1;
- DBGrid1, connected to DataSource1;
- DBNavigator1, also connected to DataSource1.

Firstly, make a backup copy of your old project, say in C:\Contries-Old.
Next, launch Delphi and open project Contries.dpr in folder C:\Contries.
Next, apply the following changes:

1. Drop a ClientDataSet component on the form.

2. Populate ClientDataSet1 with the data of Table1. Right click ClientDataSet1 and select Assign Local Data. In the dialog that opens, select Table1. Delphi reads all the records of Contries.dbf into memory.

3. Let's save the data of ClientDataSet1 to an XML file. Right click ClientDataSet1 and select Save to MyBase Xml table. In the "Save As" dialog that opens, navigate to the folder where you want to save the XML file (for example C:\Contries, the folder where our project is located) and enter a name for the file (for example, Contries).

4. In the Object Inspector, change the property ClientDataSet1.FileName to C:\Contries\Contries.xml (easy: just click the small button "..." next to the property FileName and pick the file in the dialog box "Open MyBase table").

5. Next, hook up ClientDataSet1 instead of Table1: in the Object Inspector, simply change DataSource1.DataSet to ClientDataSet1. By now, if ClientDataSet1's property Active is True, you should already see the data in the grid.

6. Delete Table1 from your form.

7. Test your project: press F9 to compile and run. If somewhere in your source there are references to Table1 (for example, Table1.Open), these will be noted by Delphi's debugger as errors; if that's the case, update your code, replacing "Table1" with "ClientDataSet1".

8. Finally, prepare your project for deployment on another computer without the BDE. From the USES clause (near the top of the unit), remove the BDE-related unit DBTables and add MIDASLIB. Now it should look something like this:

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, DBCtrls, Grids, DBGrids, DB, DBClient, MIDASLIB;

Related articles


Follow Ups