Search code examples
delphifiredaczeos

Converting ConnectForm fields from ZeosLib to FireDAC


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;

Solution

  • 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.