FireDAC.ConnectionDefs Sample
This sample illustrates the different ways to define a connection to a database.
Contents
Location
You can find the ConnectionDefs sample project at:
- Start | Programs | Embarcadero RAD Studio Athens | Samples and then navigate to:
Object Pascal\Database\FireDAC\Samples\Comp Layer\TFDConnection\ConnectionDefs
- Subversion Repository:
- You can find Delphi code samples in GitHub Repositories. Search by name into the samples repositories according to your RAD Studio version.
Description
The ConnectionDefs sample shows different ways to define a connection to a database. To this end, the sample shows how to use different methods and properties such as: ConnectionDefs, AddConnectionDef, SaveConnectionDefFile, LoadConnectionDefFile or DeleteConnectionDef.
How to Use the Sample
- Navigate to the location given above and open
ConnectionDefinitions.dproj
. - Press F9 or choose Run > Run.
Files
File in Delphi | Contains |
---|---|
|
The project itself. |
|
The main form. |
Implementation
The sample implements the following features related with the connection to a database.
Create connection definition on fly
To create/add a connection definition:
with FDManager.ConnectionDefs.AddConnectionDef do begin
Name := 'MyDefNew';
DriverID := 'MSAcc';
Database := '$(FDHOME)\DB\Data\FDDemo.mdb'; // using properties
Params.Add('Tracing=True'); // using Params
AsBoolean['ReadOnly'] := True; // using AsXXX properties
// mark persistent to save this connection definition to file later
MarkPersistent;
end;
// to make new definition persistent call following:
FDManager.ConnectionDefs.Save;
Connect to database:
dbMain.ConnectionDefName := 'MyDefNew';
dbMain.Connected := True;
Delete connection definition:
var
oDef: IFDStanConnectionDef;
begin
// ...
// ...
// Delete private connection definition. Connection definition will be destroyed after this call
// and will be no more available.
oDef.Delete;
end
Set connection definition file name
Explicitly setting the ConnectionDef file name.
FDManager.ConnectionDefFileName := 'MyTest.ini’;
Load connection definition file
Load a connection definition file explicitly with the LoadConnectionDefFile method.
// Disable auto load
FDManager.ConnectionDefFileAutoLoad := False;
// set the name of a connection definition file
if AFileName <> '' then
FDManager.ConnectionDefFileName := AFileName;
// loads the connection definitions file specified by ConnectionDefFileName.
FDManager.LoadConnectionDefFile;
Add connection definition
This is similar to create connection definition. The difference is that you may create connection definition after loading other connection definitions from file.
Clone Connection definition
To clone the connection definition you have to create the new one, assign the Name property and load the parameters of the cloned definition.
procedure TfrmConnectionDefinitions.btnTestConn5Click(Sender: TObject);
var
oDef: IFDStanConnectionDef;
begin
oDef := FDManager.ConnectionDefs.ConnectionDefByName(cbConnectionDefs5.Text);
with FDManager.ConnectionDefs.AddConnectionDef do begin
// Set new name
Name := edtName.Text;
// Clone connection definition simple assigning the parameters
Params.AddStrings(oDef.Params);
end;
end;
Forcing errors
Two give some error examples, the sample force two possible errors working with connection definitions. The error examples are the following:
- A user tries to generate connection definitions with the same name.
with FDManager.ConnectionDefs.AddConnectionDef do begin
Name := 'MyDef';
Params.DriverID := 'MSAcc';
end;
with FDManager.ConnectionDefs.AddConnectionDef do begin
Name := 'MyDef';
end;
- A user tries to load a connection definition file after creating a on fly connection.
with FDManager.ConnectionDefs.AddConnectionDef do begin
Name := 'MyOnFlyDef';
Params.DriverID := 'MSAcc';
end;
FDManager.LoadConnectionDefFile;
Uses
- TFDConnection
- ConnectionDefs
- AddConnectionDef
- LoadConnectionDefFile
- SaveConnectionDefFile
- DeleteConnectionDef
- ConnectionDefFileLoaded
- ConnectionDefFileAutoLoad
- ConnectionDefFileName
See Also
- FireDAC
- Defining Connection (FireDAC)
- Setting up Connections (FireDAC)
- Database Connectivity (FireDAC)
Samples
- FireDAC Info Report sample
- FireDAC Transactions sample