TDatabase sets up a persistent connection to a database, especially a remote database requiring a user login and password. TDatabase is especially important because it permits control over database transaction processing with the BDE when connected to a remote SQL database server. Use TDatabase when a BDE-based database application requires:
- Persistent database connections
- Customized database server logins
- Transaction control
- Application-specific BDE aliases
To connect to databases with TDatabase
- Choose FileNewOther. The New Items dialog appears.
- In the New Items dialog, select Delphi Projects and double-click VCL Forms Application. The Windows Designer displays.
- Associate a database component with a session.
- Identify the database.
- Open a connection using TDatabase.
To associate a database component with a session
- From the BDE category of the Tool Palette, drag a TDatabase component to the form.
- Drag a TSession component to the form.
- In the Object Inspector, set the SessionName property of the TSession component. SessionName is set to “Default," which means it is associated with the default session component that is referenced by the global Session variable.
- Add a TSession component for each session if you use multiple sessions.
- Set the SessionName property of the TDatabase component to the SessionName property of the TSession component to associate your dataset with a session component.
- Read the Session property to access the session component with which the database is associated at runtime. If SessionName is blank or “Default," the Session property references the same TSession instance referenced by the global Session variable.
Session enables applications to access the properties, methods, and events of a database component’s parent session component without knowing the session’s actual name. If you are using an implicit database component, the session for that database component is the one specified by the dataset’s SessionName property.
To identify the database
- In the drop-down lists for dataset components, specify the alias name or the name of an existing BDE alias for a database component.
Note: This clears any value already assigned to DriverName. Alternatively, you can specify a driver name instead of an alias when you create a local BDE alias for a database component using the DatabaseName property. Specifying the driver name clears any value already assigned to AliasName. To provide your own name for a database connection, set the DatabaseName. To specify a BDE alias at designtime, assign a BDE driver.
- Create a local BDE alias.
- Double-click a database component. The Database editor opens.
- In the Name edit box in the properties editor, enter the same name as specified by the DatabaseName property.
- In the Alias name combo box, enter an existing BDE alias name or choose from existing aliases in the drop-down list.
- To create or edit connection parameters at designtime, do one of the following:
- Use the Database Explorer or BDE Administration utility.
- Double-click the Params property in the Object Inspector to invoke the Value List editor.
- Double-click a database component in a data module or form to invoke the Database editor.
Note: All of these methods edit the Params property for the database component. When you first invoke the Database Properties
editor, the parameters for the BDE alias are not visible. To see the current settings, click
Defaults. The current parameters are displayed in the Parameter overrides memo box. You can edit existing entries or add new ones. To clear existing parameters, click
Clear. Changes you make take effect only when you click
OK.
To open a connection using TDatabase
- In the Params property of a TDatabase component, configure the ODBC driver for your application.
- To connect to a database using TDatabase, set the Connected property to True or call the Open method.
Note: Calling TDatabase. Rollback does not call TDataSet. Cancel for any data sets associated with the database.