Connecting to Databases with TDatabase

From RAD Studio
Jump to: navigation, search

Go Up to How To Perform Database Procedures

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

  1. Choose File > New > Other. The New Items dialog appears.
  2. In the New Items dialog, select Delphi Projects and double-click VCL Forms Application. The Windows Designer displays.
  3. Associate a database component with a session.
  4. Identify the database.
  5. Open a connection using TDatabase.

To associate a database component with a session

  1. From the BDE category of the Tool Palette, drag a TDatabase component to the form.
  2. Drag a TSession component to the form.
  3. 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.
  4. Add a TSession component for each session if you use multiple sessions.
  5. Set the SessionName property of the TDatabase component to the SessionName property of the TSession component to associate your dataset with a session component.
  6. Read the Session property to access the session component with which the database is associated at run time. 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

  1. 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 design time, assign a BDE driver.

  2. Create a local BDE alias.
  3. Double-click a database component. The Database editor opens.
  4. In the Name edit box in the properties editor, enter the same name as specified by the DatabaseName property.
  5. In the Alias name combo box, enter an existing BDE alias name or choose from existing aliases in the drop-down list.
  6. To create or edit connection parameters at design time, 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

  1. In the Params property of a TDatabase component, configure the ODBC driver for your application.
  2. To connect to a database using TDatabase, set the Connected property to True or call the Open method.

Note: Calling Rollback does not call Cancel for any data sets associated with the database.

See Also