1

Delphi 10.2.2

1. Is this correct way to convert this code part for MySQL from ZeosLib to FireDAC?

ZeosLib:

procedure TMeConnectForm.edExit(Sender: TObject);
begin
  try
    MainForm.MyTrinityConnection.Password := edPassword.Text;
    MainForm.MyTrinityConnection.HostName := edServer.Text;
    MainForm.MyTrinityConnection.User := edUsername.Text;
    MainForm.MyTrinityConnection.Database := edmDatabase.Text;
    MainForm.MyTrinityConnection.Port := StrToIntDef(edPort.Text, 3306);
  except
    ActiveControl := Sender as TWinControl;
    raise;
  end;
end;

FireDAC:

procedure TMeConnectForm.edExit(Sender: TObject);
begin
  try
    MainForm.MyTrinityConnection.Open('Password := edPassword.Text');
    MainForm.MyTrinityConnection.Open('Server:= edServer.Text');
    MainForm.MyTrinityConnection.Open('User_Name:= edUsername.Text');
    MainForm.MyTrinityConnection.Open('Database:= edmDatabase.Text');
    MainForm.MyTrinityConnection.Open('Port:= StrToIntDef(edPort.Text, 3306)');
  except
    ActiveControl := Sender as TWinControl;
    raise;
  end;
end;

2. Should somewhere here insert also this?

MainForm.MyTrinityConnection.DriverName:='MYSQL';




Final code after Victorias solution, looks so:

procedure TMeConnectForm.edExit(Sender: TObject);
begin
  try
    MainForm.MyTrinityConnection.DriverName:='MySQL';
    MainForm.MyTrinityConnection.Params.AddPair('Server', edServer.Text);
    MainForm.MyTrinityConnection.Params.AddPair('Port', edPort.Text);
    MainForm.MyTrinityConnection.Params.AddPair('Database', edmDatabase.Text);
    MainForm.MyTrinityConnection.Params.AddPair('User_Name', edUsername.Text);
    MainForm.MyTrinityConnection.Params.AddPair('Password', edPassword.Text);
  except
    ActiveControl := Sender as TWinControl;
    raise;
  end;
end;
Goaul
  • 943
  • 11
  • 13
  • Yes. You could also just double-click MyTrinityConnection on the design form and set values there – Vancalar Aug 22 '18 at 07:52
  • Had there everything set up, but FireDAC was refusing to use it, so worked only after putting this code in correct place: MainForm.MyTrinityConnection.DriverName:='MySQL'; – Goaul Aug 22 '18 at 14:19

1 Answers1

1

1. Should I specify DriverName for my connection?

Yes, you have to, if that connection object does not have driver yet specified.

2. How to setup connection definition parameters?

Your translation is wrong. You are just repetitively calling Open method, passing every time a single connection definition parameter (what's more, values you are passing are part of Delphi statements). That is not how it's supposed to work.

If you wanted to use the Open method to specify connection definition parameters, you would have to pass there a fully qualified connection string.

Well, to specify connection definition parameters separately you could either do so in a name value pair way through the Params collection (see Connection Definition Parameters for descriptions), for example:

FDConnection1.Params.Clear;
FDConnection1.Params.AddPair('Server', edServer.Text);
FDConnection1.Params.AddInt('Port', StrToIntDef(edPort.Text, 3306));
FDConnection1.Params.AddPair('Database', edmDatabase.Text);
FDConnection1.Params.AddPair('User_Name', edUsername.Text);
FDConnection1.Params.AddPair('Password', edPassword.Text);

Optionally, you could do the same through the specific MySQL implementation of Params collection, TFDPhysMySQLConnectionDefParams (this class you can remember from my last answer), e.g.:

uses
  FireDAC.Phys.MySQLWrapper;

var
  MySQLDef: TFDPhysMySQLConnectionDefParams;
begin
  MySQLDef := TFDPhysMySQLConnectionDefParams(FDConnection1.Params);
  MySQLDef.Server := edServer.Text;
  MySQLDef.Port := StrToIntDef(edPort.Text, 3306);
  MySQLDef.Database := edmDatabase.Text;
  MySQLDef.UserName := edUsername.Text;
  MySQLDef.Password := edPassword.Text;
end;

For generic reading about connection definitions refer to the Defining Connection topic.

Victoria
  • 7,822
  • 2
  • 21
  • 44
  • 1
    Thank you. Posted also working code so anyone can use that info. – Goaul Aug 22 '18 at 14:23
  • 1
    You're welcome! Anyway, it's not needed to `Clear` the parameters if you change the `DriverName` as it changes the internal collection. So either `Clear` or change the `DriverName` ;-) – Victoria Aug 23 '18 at 11:58