C++ Builder Tutorials

C++Builder - ClientDataSet, part 1

Database programming with C++ Builder Starter

The TClientDataSet is a component that holds data in an in-memory table. In the past, it was only available in the more expensive editions of C++ Builder. Now, however, it is also available in the free Starter edition. So, contrary to popular belief, the Starter edition does have some database components, such as TClientDataSet, TDataSource, TDBGrid, TDBEdit, and more.

You can navigate, index, search, filter, and edit the data held in memory. Because these operations are performed on data stored in memory, they are blazingly fast. For example, on a simple PC an index was built on a table with 100,000 records containing random strings of 20 characters... in less than half a second! Once built, this index can be used to perform near instantaneous searches.


Database terminology

A database is an organized collection of information, contained in tables.

    A table contains records, and each record contains fields.
    Each row of the table is a record, and each column is a field.

    Post a record means: write the data to the database.

    In this series of tutorials, we will build a flat database for maintaining the stock of articles for a furniture shop.

      Fields      
      ID Name Price Stock
    Records 0001 chair 25 50
      0002 table 50 20
      0003 desk 100 20



    Setup of the database

    Preparations

    1. If you haven't done so already, create a new folder \CppProjects on your disk.
    2. Create folder XMLDatabase1 "under" \CppProjects.
    3. Download XMLDatabase1.zip (source code for part 1) and unzip it to folder XMLDatabase1.


    Setting up the fields of the TClientDataSet

    We shall store the data in a TClientDataSet, named cdsArt.
    Each article record contains 4 fields: ID, Name, Price and Stock.

    1. In C++ Builder, right click on cdsArt
    2. Select Fields Editor...
      Note that we already have set up the fields.
      Just in order for getting the hang of it, remove the 3 fields Name, Price and Stock.
    3. Next, right click in the Fields Editor and select New Field...
    4. For the Name, enter: Name
    5. For the Type, select String
    6. For the Size, enter 30
    7. Click OK.

      Fields Editor
    8. Again, select New Field... and now specify:
      Name: Price
      Type: Float
    9. Select New Field... and specify:
      Name: Stock
      Type: Integer
    10. Close the Fields Editor.

    Compilation and test

    1. Run the project: menu Run / Run (or press function key F9).
    2. The program XMLDatabase1.exe is launched.
    3. In the navigator, click the Insert record button (the one with the red + sign) and enter some data, for example: 0001, chair, 25, 50.
    4. Click the Post edit button.
    5. Add a second record, for example: 0002, table, 50, 20.
    6. Modify a few records and delete some records.

    Limitations

    Setting up the database in this way is quite easy and almost includes no code writing.
    That's all right for a quick-and-dirty in-memory database.

    But when you play with the program, you'll note the following serious limitations:

    1. The database is not saved to a file between sessions.
    2. The database integrity is not ensured.
      For example, you can have articles with the same ID.
      You can also have records with empty fields, a negative price or a negative stock.
    3. When you are in the process of inserting or editing a record, clicking on another row automatically "posts" the data.
    4. It's much too easy to overwrite existing data by simply typing over them.

    We'll improve on all of this in the next parts of our tutorial.


    About the author

    Industrial Engineer Guido Festra worked as a profesionnal hardware- and software-instructor for various American computer companies (Varian Data Machines, Texas Instruments, Wang Computers and others). Nowadays, he develops custom software and websites for European companies.
    Guido is the author and main maintainer of the website festra.com, presenting Delphi and C++ Builder articles and tutorials. He is also the author of numerous software manuals and ITC courses.