C++Builder, 1, Borland C++Builder White Paper: Intro to Data Components

By: Borland Staff

Abstract: Borland C++Builder offers developers unparalleled power and flexibility for application development.

Introduction to the Database Components in Borland C++Builder
Written by Richard Lussier. Revised by Jeff Cottingham

Introduction

Borland C++Builder offers developers unparalleled power and flexibility for application development. In particular, the Borland Visual Component Library's built-in database components provide an incredibly easy-to-use set of tools for database application development. For a large part of the development process, the programmer simply points and clicks to set options for various components. In fact, as we'll see, a client/server one-to-many database form can be created rapidly and with little effort.

Database components also offer versatility. The components can be programmed to respond to events, and options can be set at runtime which transform a simple application into a very professional one.

This paper will take an in-depth look at some of the components and tools through which C++Builder supports database application development. Specifically, the following issues will be covered:

  1. A review of the C++Builder database development philosophy
  2. Introduction of the TDataSet, TTable, TQuery, and TDataSource components
  3. Demonstration of the data-aware controls including TDBEdit and TDBCombobox
  4. Review the power and flexibility of the DBNavigator component
  5. Discussion of the Database Form Wizard
  6. Demonstration of parameterized queries

All discussion will be occur in the context of a client/server environment to assist the reader of this paper in quickly developing an understanding of C++Builder's client/server development capabilities.

Borland C++Builder Database Development

C++Builder is a general purpose Windows 95 and NT development tool. It can be used to create standalone EXEs, DLLs, drivers, games - virtually anything. Borland C++Builder's versatility is due to its powerful language, C++, and its rich set of tools. Many other development environments are specifically targeted towards a particular type of development. C++Builder offers flexibility.

One of the strongest aspects of the Borland C++Builder development environment is its database application development toolset. C++Builder offers the corporate developer, consultant, or hobbyist an extensive array of tools and utilities geared specifically towards database applications. Most significantly, Borland C++Builder offers database development related components. Components are reusable, generic objects that are used and/or built in C++Builder and are incorporated into C++Builder applications. Components can be compared to OCX and VBX controls, which may also be manipulated within a visual environment, but components can provide better integrated, and often more extensive, capabilities.

A major difference between VBX controls and components is that components are compiled into the final executable that Borland C++Builder produces, while VBXs remain separate and must be distributed with the application. Thus, performance can suffer. Many 'data-aware' VBX controls are great for developing very simple applications. They provide a means for developing or rapidly prototyping database applications. Many experienced developers of more sophisticated applications will choose, however, to call native APIs rather than use such controls. This can provide a more stable and much faster application, if done properly. A significant drawback to this approach is the amount of code required to create even simple applications. In addition, only more advanced developers have the knowledge and experience necessary to make direct API calls reliably.

Borland C++Builder's data-aware components are different. In developing the Visual Component Library components, Borland has ensured that they are easy to use, flexible, and fast. Because C++Builder can create an executable, using the database components can provide the stability and speed of direct API calls.

C++Builder provides two types of database-related components: data aware controls and data access components.

  • Data-aware controls such as TDBEdit (a edit region or enterable field) and TDBGrid (a spreadsheet like display table) provide the display of data on a form. They allow the user of the application to look at, enter, change, or delete data.
  • Data access components include Dataset components such as TTable and TQuery. These fundamental data access components point to the actual data residing in a database file.

Data access components also include TDataSource components which provide a layer of 'abstraction' on top of the Dataset components. Borland C++Builder supports 'three-tiered' application development in which a data-aware control is linked to a data source component which is in turn linked to actual data via a table or query component. The middle tier in this approach, TDataSource, allows the actual table that a form is based upon to be changed at design time without any redesign of the application - all display elements are linked to TDataSource, not the underlying table or query. Similarly, the underlying table can be changed on the fly at runtime with only a trivial amount of code. TDataSource components can also make it easy to provide essential functionality such as synchronized data across more than one entry form.

The topics below delve into each of these components in detail. A section on 'putting the components together' follows.

It should be noted that one additional 'component' contributes to the database development support of Borland C++Builder - the underlying data engine, Borland Database Engine (BDE). BDE is a unifying technology in all Borland products: C++Builder and Borland C++, Delphi, IntraBuilder, and JBuilder. BDE allows the developer to gain access to Paradox, dBASE or any ODBC data sources transparently. Using Borland SQL Links native access to many SQL servers is also supported. A data-centric application need only know the name of an 'alias' for any database to have access to that database's contents. (Aliases are set up in the BDE Configuration Utility (BDECFG.EXE) and specify driver parameters and database locations.)

Data Access Components

As noted above, the data access components provide a means for the developer to indicate where the data to be presented resides. These components include TTable, TQuery and TDataSource.

TTable
TTable defines a link to a database table. With this tool, all the data from a table is available to the application. TTables are useful for most application needs.

Placing a TTable component on a form requires the developer to specify some basic properties using the Object Inspector. Below are descriptions of the most common properties of the TTable Object Inspector:

    Active: This property specifies whether the connection to the table is active during design mode. One of the most useful features of the Borland C++Builder database components is that as a developer, you can see the actual data in design mode. This makes it much easier to see how a form will look like at run-time.

    DatabaseName: The database name is either a DOS path or a BDE alias where the database resides. An alias is always preferred over a hard-coded path. By using an alias you can change the physical location of the data, for example, from a local drive to a network drive, without having to recompile the application - simply change the alias Path property in the BDE Configuration Utility. If you are developing using a client/server database, an alias is necessary to specify the various parameters that the database server will require.

    IndexName: By specifying the IndexName property, you can display the data in an order other than the primary key order, which, if there is a primary key, will be the default.

    ReadOnly: This property limits write access to table data and can be used to control access rights at run-time.

    TableName: The table name is the actual table in which the data resides. By dropping down the combobox in the property editor, you will see all the tables in the directory pointed to by the DatabaseName property. If you are working on a client/server database, you will see all the table names in the server's database pointed to by the alias.

TQuery
TQuery is similar to TTable in that it points to the actual data table. Unlike TTable, TQuery allows the developer to specify which records to retrieve, which columns to retrieve, and whether the data resides in two or more tables. The means of doing this is through queries written in SQL, the industry-standard query language. With SQL, developers can write very sophisticated queries against their databases. Borland C++Builder passes queries to the database's processing engine where they are interpreted and from which the results are returned to the C++Builder application.

The key properties that must be considered for TQuerys are somewhat different than those for TTables.

    Params: Parameters can be used in a query in order to make the query more generic. The Params property allows the developer to specify the datatypes and default values of parameters used in a query. Additional information follows below.

    RequestLive: BDE has the ability to create 'live' updatable queries for databases that support the necessary pointer or cursor functionality.

    SQL: The SQL property allows you to specify the SQL query. If you double-click on this property, a List Editor window will appear which presents an editor. Simply type in the SQL command for the TQuery and click the OK button.

    Active: Similar to the Active property of the TTable, the Active property of the TQuery opens the DataSet and allows the developer to see the actual data that is returned to the application from the query during design or runtime mode.

    DatabaseName: This property is required in order for the query to know where the data necessary to resolve the query resides. An alias is preferred over a hard-coded path.

TDataSource
As discussed earlier, the TDataSource component provides a link between the dataset objects and the visual display, or data-aware controls.

    AutoEdit: This property specifies whether or not changes to a data-aware control automatically put the record into "Edit" mode. In most cases this property should be set to True, the default.

    DataSet: With this property, the developer specifies to which dataset component this data source is linked. As we'll see later, each data-aware component has a DataSource property which would complete the three-tiered relationship. The DataSource property in the TTable and TQuery objects allows the developer to easily create one-to-many forms. By hooking the detail table's DataSet object to the master table's DataSource, Borland C++Builder and BDE will automatically keep the master-detail relationship in sync.


Server Response from: ETNASC04