Searching for a Database Connection

From RAD Studio
Jump to: navigation, search

Go Up to Managing database sessions Index

Attention: The Borland Database Engine (BDE) has been deprecated, so it will not be enhanced. For instance, BDE will never have Unicode support. You should not undertake new development with BDE. Consider migrating your existing database applications from BDE to dbExpress.

Use a session's FindDatabase method to determine whether a specified database component is already associated with a session. FindDatabase takes one parameter, the name of the database to search for. This name is a BDE alias or database component name. For Paradox or dBASE, it can also be a fully qualified path name.

FindDatabase returns the database component if it finds a match. Otherwise it returns nil.

The following code searches the default session for a database component using the DBDEMOS alias, and if it is not found, creates one and opens it:

var
  DB: TDatabase;
begin
  DB := Session.FindDatabase('DBDEMOS');
  if (DB = nil) then                           { database doesn't exist for session, so}
    DB := Session.OpenDatabase('DBDEMOS');     { create and open it}
  if Assigned(DB) and DB.Connected then begin
    DB.StartTransaction;
    ...
  end;
end;
TDatabase *DB = Session->FindDatabase("BCDEMOS");
if ( !DB )                               // Database does not exist for session, so
  DB = Session->OpenDatabase("BCDEMOS"); // create and open it
if (DB && DB->Connected)
{
  if (!DB->InTransaction)
  {
    DB->StartTransaction();
    .
    .
    .
  }
}

See Also