Connecting to a Database Server

From RAD Studio
Jump to: navigation, search

Go Up to Controlling Connections


There are two ways to connect to a database server using a connection component:

Calling the Open method sets Connected to True.

Note: When a connection component is not connected to a server and an application attempts to open one of its associated datasets, the dataset automatically calls the connection component's Open method.

When you set Connected to True, the connection component first generates a Data.DB.TCustomConnection.BeforeConnect event, where you can perform any initialization. For example, you can use this event to alter connection parameters.

After the BeforeConnect event, the connection component may display a default login dialog, depending on how you choose to control server login. It then passes the user name and password to the driver, opening a connection.

Once the connection is open, the connection component generates an Data.DB.TCustomConnection.AfterConnect event, where you can perform any tasks that require an open connection.

Note: Some connection components generate additional events as well when establishing a connection.

Once a connection is established, it is maintained as long as there is at least one active dataset using it. When there are no more active datasets, the connection component drops the connection. Some connection components surface a KeepConnection property that allows the connection to remain open even if all the datasets that use it are closed. If KeepConnection is True, the connection is maintained. For connections to remote database servers, or for applications that frequently open and close datasets, setting KeepConnection to True reduces network traffic and speeds up the application. If KeepConnection is False, the connection is dropped when there are no active datasets using the database. If a dataset that uses the database is later opened, the connection must be reestablished and initialized.

See Also