1097-1101

Previous Table of Contents Next

Page 1097

CHAPTER 49

Using Delphi

IN THIS CHAPTER

  • Configuring a Data Source 1098
  • Communicating with the Database 1100

Page 1098

Borland's Delphi is one of the newest visual development tools for Microsoft Windows. It has positioned itself in the market as a high performance alternative to Microsoft Visual Basic. What separates Delphi from its competition in this regard is its capability to compile standalone executables and .DLLs with no runtime libraries required.

Delphi provides full support of ODBC and numerous object classes for communicating with ODBC data sources. It ships with a single- user desktop version of Borland's InterBase Server and its ODBC-enabled ReportSmith querying and reporting tool. The client/server edition also ships native IDAPI drivers for Oracle, Sybase, and Informix, among others.

Object Pascal, the programming language used in Delphi development, supports many of the object-oriented features typically associated with C++, including public and private data members and functions, inheritance, and polymorphism. The language also supports a wide range of datatypes and advanced features, including true pointer types and structured exception handling.

Although Object Pascal and Delphi provide an abundance of noteworthy features, this chapter focuses primarily on Delphi's support for database applications. An overview of the more significant features of the language is included, as well as a brief discussion of the strengths and weaknesses of Delphi.

Configuring a Data Source

To make a data source available to Delphi, you must edit IDAPI.CFG using the BDE configuration utility. This utility can be accessed from the Tools menu in the IDE. When the utility is started, it displays currently configured data sources, some of which might be preconfigured IDAPI interfaces, depending on the options selected when Delphi was installed. The main window of the BDE configuration utility should appear as shown in Figure 49.1.

Figure 49.1.
The main window of
the Delphi BDE
configuration utility.

Page 1099

Delphi can interface with any ODBC data source configured and defined in ODBC.INI. To configure an existing ODBC data source for Delphi's use, click the New ODBC Driver command button. A simple dialog box displays and requires three basic items. The SQL Link Driver name is simply an identifier to be used by Delphi, and you can use any unique name . Using the Default ODBC Driver drop-down list, select from the list of currently installed drivers. If the required driver does not appear in the list, it was not correctly installed. You can install ODBC drivers using ODBCADMN.EXE, a Microsoft utility that is typically provided by driver vendors . After you select a driver, the Default Data Source Name drop-down list is populated with a list of the data sources defined for the selected driver. Figure 49.2 provides an example of how this dialog box might look when you configure the Oracle ODBC driver. The Oracle ODBC driver requires SQL*Net to be installed.

Figure 49.2.
The Add ODBC
Driver dialog.

After selecting the default data source, click the OK button to add the driver. The newly added driver should now appear in the main window of the configuration utility, as shown in Figure 49.3.

Figure 49.3.
The main window of
the configuration
utility.

Of the available parameters, SQLQRYMODE and SQLPASSTHRU MODE are the most significant. Setting SQLQUERYMODE to SERVER causes Delphi to deliver all queries to the database server for processing. This prevents the local BDE from attempting to process the query from the desktop.

Page 1100

The SQLPASSTHRU mode should be set to SHARED NOAUTOCOMMIT. This setting prevents transactions sent to the server from being committed automatically. The tabs across the bottom of the BDE configuration utility's main window allow the setting of additional parameters, including date, time, and numeric formats. These settings apply to the local database engine and should not need to be modified for ODBC connections.

The BDE configuration utility supports multiple configuration files. The changes can be saved in a new file, and the utility will optionally update WIN.INI to use the new file as the default. On Windows NT and Windows 95, this is saved as a registry entry. When adding a new data source, you might want to save the configuration file with a different name so that you can use the old file as a backup, in case other data sources were changed inadvertently. After the configuration file is saved, Delphi can access the new data source.

Communicating with the Database

Delphi supplies several classes that you can use to establish and maintain connections to the database, process transactions, and retrieve result sets. The most useful of these classes include

  • TSession: Used to manage database connections
  • TDatabase: Used to establish a connection and control transactions
  • TQuery: Subclass of TDataSet, used to retrieve results and apply transactions
  • TStoredProc: Subclass of TDataSet, used to call stored procedures
  • TDataSource: Used to bind data-aware controls to result sets and to link related result sets
  • TField: Corresponds to a column in a result set

All these objects are available on the Data Access tab of the toolbar.

Communication with the database is initiated by the TDatabase component. Before establishing a connection, you must set the DriverName and DatabaseName properties. These should correspond to the driver name (specified when the data source was set up through BDE configuration) and a valid data source name for the specific driver. The application connects to the database when you use the Open method or set the Connected property to True.

TIP
Although you can access all objects' properties and methods through the Object Inspector, some objects and properties have special dialog boxes. For example, double-clicking the TDatabase component displays a dialog box that makes it easier to view and modify its properties.

Page 1101

If the Params property does not have all the required parameters set, the TDatabase object displays a login form automatically, provided that the LoginPrompt is set to True. Connection parameters are set using the Params property, which is actually a TStrings object. Parameters are set using the Add method, as demonstrated in the following code segment:

 dbOra.Params.Add(`SERVER NAME=ORACLE'); dbOra.Params.Add(`USER NAME=scotty'); dbOra.Params.Add(`PASSWORD=tiger'); dbOra.Connected := True; 

The KeepConnected property, when set to True, keeps the connection open even when no result sets are open and no transactions are in progress. The TransIsolation property determines how records are read when they are currently involved in a transaction. Oracle supports tiReadCommitted, which allows only committed changes to be read, and tiRepeatableRead in read-only mode. The latter mode prevents Oracle from attempting to refresh the snapshot for records that have already been read.

The TDatabase object is also used for transaction control ( assuming that SQLPASSTHRU MODE was set to SHARED NOAUTOCOMMIT when the data source was configured). You should use the StartTransaction, Commit, and Rollback methods whenever SQL transactions are applied. The exception to this rule is when the application calls a stored procedure that handles commits and rollbacks internally.

In some cases, the behavior of the TDatabase object can be overridden by the TSession object. The TSession object is particularly useful for applications that connect to more than one database or need more than one connection for asynchronous processing. The KeepConnections property of the TSession object overrides the KeepSessions property of individual TDatabase objects. The TSession object is always global and has additional methods for querying the BDE configuration files. It stores an array of connected TDatabase objects in its Databases property and the number of connected TDatabase objects in its DatabaseCount property.

The TQuery object is the primary means for communicating with the database. It is used not only to retrieve results, but also to apply SQL transactions. The SQL property is a TStrings object used to store the SQL to be executed. An application can use a TQuery to build simple dynamic SQL easily using the Params property. Application variables can be substituted for literal values wherever they might appear. For example, you can use the following statements to generate a result set dynamically based on a value entered by a user in a TEdit object:

 qryGetCusts.SQL.Add(`SELECT * FROM CUSTOMERS WHERE LAST_NAME LIKE :LastName'); qryGetCusts.Params[0].AsString(txtLastName.Text); 

This is particularly useful for data entry forms where records are inserted frequently into the same table or view. The Params property is an array of TParams objects. They are referenced by subscripts and set using the AsString, AsInteger, AsFloat, or one of the other properties that store a value based on a datatype.

Previous Table of Contents Next


Oracle Unleashed
Oracle Development Unleashed (3rd Edition)
ISBN: 0672315750
EAN: 2147483647
Year: 1997
Pages: 391

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net