Installing "Server" programmatically with TFDConnection

TFDConnection.Params.Server is not a valid published property in Delphi XE7. How to set server location programmatically? I have 2 MySQL servers (test and production) that have different IP addresses and depending on what I do in the application, I want to easily switch between the two servers.

+5
source share
3 answers

Please read the documentation that describes how to determine the FireDAC connection for MySQL:

Work with connections (FireDAC)

Connect to MySQL Server (FireDAC)

You must specify the database server as part of the connection definition :

Connection Definition (FireDAC)

.ini, TFDManager.ConnectionDefFileName TFDManager.LoadConnectionDefFile().

[MySQL_Connection_1]
DriverID=MySQL
Server=192.168.1.100
...

[MySQL_Connection_2]
DriverID=MySQL
Server=192.168.1.101
...

TFDManager.ConnectionDefs:

var
  oDef: IFDStanConnectionDef;
begin
  oDef := FDManager.ConnectionDefs.AddConnectionDef;
  oDef.Name := 'MySQL_Connection_1';
  oDef.DriverID := 'MySQL';
  oDef.Server := '192.168.1.100';
  ...
  oDef.Apply;

  oDef := FDManager.ConnectionDefs.AddConnectionDef;
  oDef.Name := 'MySQL_Connection_2';
  oDef.DriverID := 'MySQL';
  oDef.Server := '192.168.1.101';
  ...
  oDef.Apply;

var
  oParams: TStrings;
begin
  oParams := TStringList.Create;
  oParams.Add('Server=192.168.1.100');
  ...
  FDManager.AddConnectionDef('MySQL_Connection_1', 'MySQL', oParams);

  oParams.Clear;
  oParams.Add('Server=192.168.1.101');
  ...
  FDManager.AddConnectionDef('MySQL_Connection_2', 'MySQL', oParams);

TFDConnection, :

FDConnection1.ConnectionDefName := 'MySQL_Connection_1';
// or: FDConnection1.ConnectionDefName := 'MySQL_Connection_2';
FDConnection1.Connected := True;

TFDConnection.Params, :

FDConnection1.DriverName := 'MySQL';
FDConnection1.Params.Clear;
FDConnection1.Params.Add('Server=192.168.1.100');
// or: FDConnection1.Params.Values['Server'] := '192.168.1.100';
...
FDConnection1.Connected := True;
+8

.

var
oParams: TStrings;
begin
oParams := TStringList.Create;
oParams.Add('Server=' + YourServer);
oParams.Add('Database=' + YourDatabase);
oParams.Add('OSAuthent=Yes');

FDManager.AddConnectionDef('CNX1', 'MSSQL', oParams);
FDConnection.ConnectionDefName := 'CNX1';
FDConnection.Connected := true;
if FDConnection.Connected then
ShowMessage('Connected');
oParams.Free;
0

, .

, TLama :

, MySQL, PARAMS . :

(Conn1.Params as TFDPhysMySQLConnectionDefParams).Server := '127.0.0.1';

, .

0

All Articles