FireDAC.IFDPhysConnection.CreateConnection Sample

From RAD Studio Code Examples
Jump to: navigation, search

This sample demonstrates the use of the IFDPhysConnection and IFDPhysManager interfaces to connect to a database in FireDAC.


You can find the CreateConnection sample project at:

  • Start | Programs | Embarcadero RAD Studio Alexandria | Samples and then navigate to:
    • Object Pascal\Database\FireDAC\Samples\Phys Layer\IFDPhysConnection\CreateConnection
  • Subversion Repository:
    • You can find Delphi code samples in GitHub Repositories. Search by name into the samples repositories according to your RAD Studio version.


The CreateConnection sample shows you how to use the IFDPhysConnection and IFDPhysManager interfaces in order to connect to a database in FireDAC. To this end, the sample creates a database connection in four different ways:

  1. Using a persistent connection definition.
  2. Using a parent definition.
  3. Defining the connection parameters to create a new connection definition in run time.
  4. Using the FireDAC connection string to create a new connection definition in run time.

How to Use the Sample

  1. Navigate to the location given above and open CreateConnection.dproj.
  2. Press F9 or choose Run > Run.


File in Delphi Contains


The project itself.


The main form.


This sample uses the CreateConnection method of IFDPhysManager to create a database connection in four different ways. When you run the application, you see two objects: a TMemo and a TButton. Click the Create Connection button to create the connections. For each one of the four different connections, the sample repeats the following process:

  1. The sample creates a conection.
  2. The sample opens the connection.
  3. The sample uses the memo object to notify it.
  4. The sample closes the connection.

The sample creates the four different connections as follows:

Using a persistent connection definition
The sample creates the first connection to a database using an existing permanent connection definition. To this end, the sample uses the CreateConnection method to select the Access_Demo connection definition. Then, the sample calls the Open and Close methods of IFDPhysConnection to open and close the connection.
Note: The persistent connection definitions are stored in an external file - the connection definition file. By default the file is C:\Users\Public\Documents\Embarcadero\Studio\FireDAC\FDConnectionDefs.ini.
Using a parent definition
To create the second connection, the sample makes use of the ParentDefinition property to reuse the Access_Demo connection definition. The sample changes some parameters from the Access_Demo connection and renames it as MyDefOverride. Then, the sample uses the CreateConnection, Open and Close methods to connect through the MyDefOverride connection.
Note: The MyDefOverride connection is not saved as a persistent connection. Moreover, the Access_Demo connection definition remains unchanged on the connection definition file.
Creating a new connection definition in run time
The sample creates the third connection on-fly. This connection is named as MyDefnew. The sample configures its parameters in run time in order to connect through myDefNew connection.
Note: To make the myDefNew connection definition as persistent call the ConnectionDefs.Save method.
Creating new definition using FireDAC connection string
To create the last connection, the sample uses the FireDAC connection string to set the desired parameters of the connection. The parameter are separated by semicolons. See the code below:
 FDPhysManager.CreateConnection('DriverID=MSAcc;Database=$(FDHOME)\DB\Data\FDDemo.mdb;ReadOnly=True', oConn);


See Also