The ConnectionDefs property is a powerful tool that can be used to make your connections management simple and flexible, with the possibility of persisting and transport your connection’s parameters whenever you want, using these transportable files you can load and navigate between previously configured connections on self-contained .INI files, thus, you don’t have to worry about load and save settings on settings files yourself, this is all abstracted within connectionDef’s functions.
ConnectionDefs requires just the actual native FireDac installed.
To use the ConnectionDefs function just add the TFDManager on your code, after that you can:
Create connection definition on fly:
1 2 3 4 5 6 7 8 9 10 11 |
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:
1 2 |
dbMain.ConnectionDefName := 'MyDefNew'; dbMain.Connected := True; |
Delete connection definition:
1 2 3 4 5 6 7 8 9 |
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:
1 |
FDManager.ConnectionDefFileName := 'MyTest.ini’; |
Load connection definition file:
1 2 3 4 5 6 7 |
// 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 a connection definition after loading other connection definitions from the file.
Clone Connection definition:
1 2 3 4 5 6 7 8 9 10 11 12 |
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; |
You can find Delphi code samples in GitHub Repositories.
Check out the full article in the DocWiki about the ConnectionDefs.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition
The 6 samples inserted in the article is a large HTML file, and not as expected short snippets of Object Pascal / Delphi code. Not very useful as is.
Seems like there was an issue with the code snippets on this post. Looks corrected now.