AnyDAC
ContentsIndexHome
PreviousUpNext
Establishing Connection

Describes how to open and close connection to a DBMS using AnyDAC. To open a connection to a database AnyDAC offers the TADConnection component.

Group
Links
General

After a connection definition is created, the connection may be established to a database. In general, there are two ways:

  • explicitly, by setting TADCustomConnection.Connected to True or calling one of the Open methods;
  • implicitly, by performing any action requiring to talk to a DBMS. For example, by setting linked TADQuery Active property to True. Note, that ResourceOptions.AutoConnect must be True, otherwise an exception will be raised.

AnyDAC offers few TADCustomConnection.Open methods additionally to the Connected property. These method allows to use an AnyDAC connection string, which is a string in the form of param=value[;...param=value]. For example: 

 

ADConnection1.Open('DriverID=SQLite;Database=c:\test.sdb;Password=12345');

 

Before the connection will be opened the BeforeConnect event will be fired. After connection establishment - AfterConnect. 

 

Handling connection errors

If connection establishment fails, then an application may analyze failure using one of the approaches:

  • using OnError event handler. That is more appropriate when a connection is opened implicitly;
  • using try ... except ... end syntax. That is the best approach with explicit connection establishment. For example:

 

try
  ADConnection1.Connected := True;
except
  on E: EAbort do
    ; // user pressed Cancel button in Login dialog
  on E: EADDBEngineException do
    case E.Kind of
    ekUserPwdInvalid: ; // user name or password are incorrect
    ekUserPwdExpired: ; // user password is expired
    ekServerGone: ;     // DBMS is not accessible due to some reason
    else                // other issues
    end;
end;

 

Note, that Login dialog is automatically handling the ekUserPwdInvalid error kind, by suggesting to the user to repeat the login. And the ekUserPwdExpired, by allowing to the user to enter the new password. 

Also, if the connection recovery is setup, then ekServerGone error kind will lead to bringing a connection to an initially offlined state. Alternatively the Ping method may be used to avoid ekServerGone error and make the connection active, when a DBMS is available. 

See "Handling Errors" for more details. 

 

Using the Login dialog

The GUI application may use TADGUIxLoginDialog component to allow the end users to enter the database credentials. The login dialog may be bind by one of the ways:

The Login dialog will be automatically invoked by the TADCustomConnection, when LoginPrompt = True: 

 

 

 

Using VisibleItems property you may specify which connection definition parameters to show to the end user and how to name them. The last option allows to localize the Login dialog. For example, German speaking SQL Server developers may specify: 

 

with ADGUIxFormsLoginDialog1.VisibleItems do begin
  Clear;
  Add('Server');
  Add('User_name=Benutzer');
  Add('Password=Kennwort');
  Add('OSAuthent');
end;
ADConnection1.LoginDialog := ADGUIxFormsLoginDialog1;
ADConnection1.Connected := True;

 

When a DBMS supports password expiration, the password is expired and ChangeExpiredPassword is True, the dialog will ask for a new password. 

 

Closing Connection

The connection may be closed by one of the two ways:

Before connection will be closed AnyDAC will finish the active transactions, if any. Use TxOptions.DisconnectAction to control the performed action. 

Also before that the BeforeDisconnect event will be fired. After connection closing - AfterDisconnect.

What do you think about this topic? Send feedback!