FireDAC.IFDPhysConnection.CreateConnection Sample
This sample demonstrates the use of the IFDPhysConnection and IFDPhysManager interfaces to connect to a database in FireDAC.
Contents
Location
You can find the CreateConnection sample project at:
- Start | Programs | Embarcadero RAD Studio Berlin | Samples and then navigate to:
Object Pascal\Database\FireDAC\Samples\Phys Layer\IFDPhysConnection\CreateConnection
- Subversion Repository:
Description
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:
- Using a persistent connection definition.
- Using a parent definition.
- Defining the connection parameters to create a new connection definition in run time.
- Using the FireDAC connection string to create a new connection definition in run time.
How to Use the Sample
- Navigate to the location given above and open
CreateConnection.dproj
. - Press F9 or choose Run > Run.
Files
File in Delphi | Contains |
---|---|
|
The project itself. |
|
The main form. |
Implementation
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:
- The sample creates a conection.
- The sample opens the connection.
- The sample uses the memo object to notify it.
- 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
.
- Note: The persistent connection definitions are stored in an external file - the connection definition file. By default the file is
- 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 theAccess_Demo
connection and renames it asMyDefOverride
. Then, the sample uses the CreateConnection, Open and Close methods to connect through theMyDefOverride
connection.- Note: The
MyDefOverride
connection is not saved as a persistent connection. Moreover, theAccess_Demo
connection definition remains unchanged on the connection definition file.
- Note: The
- 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 throughmyDefNew
connection.- Note: To make the
myDefNew
connection definition as persistent call the ConnectionDefs.Save method.
- Note: To make the
- 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);
Uses
See Also
- FireDAC
- Database Connectivity (FireDAC)
- Defining Connection (FireDAC)
- Setting up Connections (FireDAC)