RAD Studio
ContentsIndex
PreviousUpNext
Searching for a Database Connection

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();
    .
    .
    .
  }
}
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!