FireDAC.ConnectionDefs Sample

From RAD Studio Code Examples
Jump to: navigation, search

This sample illustrates the different ways to define a connection to a database.

Location

You can find the ConnectionDefs sample project at:

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

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

Files

File in Delphi Contains

ConnectionDefinitions.dproj
ConnectionDefinitions.dpr

The project itself.

fConnectionDefinitions.pas
fConnectionDefinitions.fmx

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

See Also

Samples

Personal tools
Assistance