Describes how to store and use AnyDAC connection parameters and what is a connection definition. To specify connection parameters an application must use a connection definition. The connection definition is a set of parameters. A connection may be also pooled.
A connection definition is a set of parameters that defines how to connect an application to a DBMS using specific AnyDAC driver. It is the equivalent of a BDE alias, ADO UDL (stored OLEDB connection string) or ODBC Data Source Name (DSN). For the list of supported DBMS's and corresponding parameters see AnyDAC Database Connectivity.
AnyDAC supports 3 connection definition kinds:
|
Type |
Description |
Pros |
Cons |
|
Persistent |
Has an unique name, is managed by the ADManager and is stored in a connection definition file. |
May be defined once and reused across many applications. May be pooled. |
The parameters (server address, DB name, etc) are publically visible and may be changed incidentally. ADManager has to be be reactivated or the Delphi IDE has to be be restarted to make a newly added definition visible at design time. |
|
Private |
Has an unique name, is managed by the ADManager but is NOT stored in a connection definition file. |
Connection definition parameters are not visible "outside" the application. May be pooled. |
Application needs to create a private connection definition after each program restart and cannot share it with the other programs. Cannot be created at design time. |
|
Temporary |
Has no name, is not stored in a connection definition file and is not managed by the ADManager. |
The most simple way to create a connection definition - just fill in the TADConnection.Params property. May be created at design time using the TADConnection component editor. |
Similar to private. Also cannot be referenced by name and cannot be pooled. |
The persistent connection definitions are stored in an external file - the connection definition file. This file has the standard INI text file format. It may be edited by ADExplorer or ADAdministrator utilities at first, manually or by code. By default the file is $(ADHome)\DB\ADConnectionDefs.ini.
Note: If you added a new persistent connection definition using ADExplorer or ADAdministrator while the Delphi IDE is running, it will not be visible to the AnyDAC design time code. To refresh the persistent connection definitions list, you will need to reactivate ADManager or restart the Delphi IDE.
Sample content of this file:
[Oracle_Demo] DriverID=Ora Database=ORA_920_APP User_Name=ADDemo Password=a MetaDefSchema=ADDemo ;MonitorBy=Remote [MSSQL_Demo] DriverID=MSSQL Server=127.0.0.1 Database=Northwind User_Name=sa Password= MetaDefSchema=dbo MetaDefCatalog=Northwind MonitorBy=Remote
An application can specify a connection definition file name in the ADManager.ConnectionDefFileName property. AnyDAC searches for a connection definition file in the following places:
Note: At design time, AnyDAC ignores the value of the ADManager.ConnectionDefFileName, and looks for a file in a Delphi Bin folder or as specified in the registry. If the file is not found, an exception is raised.
If ADManager.ConnectionDefFileAutoLoad is True, a connection definition file is loading automatically. Otherwise it must be loaded explicitly by calling the ADManager.LoadConnectionDefFile method before the first usage of the connection definitions. For example, before setting TADConnection.Connected to True.
A persistent connection definition may be created using ADExplorer or ADAdministrator. Here, we will show you how to do that in code: $(ADHome)\Samples\Comp Layer\TADConnection\ConnectionDefs.
The following code snippet creates a connection definition named "MSSQL_Connection", which has all required parameters to connect to the Microsoft SQL Server running locally, using the OS authentification (SSPI):
uses uADCompClient, uADStanIntf; var oDef: IADStanConnectionDef; begin oDef := ADManager.ConnectionDefs.AddConnectionDef; oDef.Name := 'MSSQL_Connection'; oDef.DriverID := 'MSSQL'; oDef.Server := '127.0.0.1'; oDef.Database := 'Northwind'; oDef.OSAuthent := True; oDef.MarkPersistent; oDef.Apply; ..................... ADConnection1.ConnectionDefName := 'MSSQL_Connection'; ADConnection1.Connected := True;
The ADManager is a global instance of the AnyDAC connection manager. Its property ConnectionDefs: IADStanConnectionDefs is a collection of the persistent and private connection definitions. The AddConnectionDef method adds a new connection definition. The MarkPersistent method marks a connection definition as persistent. The Apply method saves a connection definition to a connection definition file. Without the MarkPersistent call, the connection definition will be private.
A private connection definition may be created only in code. The code is similar to the one above, but without the MarkPersistent call.
Also, you can use a technique similar to BDE:
var oParams: TStrings; begin oParams := TStringList.Create; oParams.Add('Server=127.0.0.1'); oParams.Add('Database=Northwind'); oParams.Add('OSAuthent=Yes'); ADManager.AddConnectionDef('MSSQL_Connection', 'MSSQL', oParams); ..................... ADConnection1.ConnectionDefName := 'MSSQL_Connection'; ADConnection1.Connected := True;
A temporary connection definition may be created at design-time using AnyDAC Connection Editor. For that double click on a TADConnection to invoke the editor:
Or at run-time in code by filling the TADConnection.Params property. This is the simplest way to create a connection definition.
ADConnection1.DriverName := 'MSSQL';
ADConnection1.Params.Add('Server=127.0.0.1');
ADConnection1.Params.Add('Database=Northwind');
ADConnection1.Params.Add('User_name=sa');
ADConnection1.Connected := True;
An application may need an ability to create and edit a connection definition at run-time using standard AnyDAC Connection Editor dialog. To edit a temporary connection definition stored in TADConnection use the code:
uses uADGUIxFormsfConnEdit; ... if TfrmADGUIxFormsConnEdit.Execute(ADConnection1, '') then ADConnection1.Connected := True;
To edit a connection definition represented as AnyDAC connection string use the code:
uses uADGUIxFormsfConnEdit; ... var sConnStr: String; ... sConnStr := ADConnection1.ResultConnectionDef.BuildString(); if TfrmADGUIxFormsConnEdit.Execute(sConnStr, '') then begin ADConnection1.ResultConnectionDef.ParseString(sConnStr); ADConnection1.Connected := True; end;
|
What do you think about this topic? Send feedback!
|