FireDAC WTF of the day ;)

FireDAC WTF of the day ;)

Conn := TFDConnection.Create(nil);
Conn.DriverName := FireDriverLink.BaseDriverId;
// The above lines were actually in a function in the worker pool

for tgt in targetlist
do begin
  Conn.Params.Clear;
  Conn.Params.Values['Server'] := tgt.Host;
  Conn.Params.Values['Database'] := tgt.DatabaseName;
  Conn.Params.Values['OSAuthent'] := 'No';
  Conn.Params.Values['User_Name'] := tgt.UserName;
  Conn.Params.Values['Password'] := tgt.Password;
  Conn.Connect;
  Exception: Driver name not set.

I added the above clear since I would be reusing the connection for multiple databases in a loop, and it took me a few rounds in the debugger before I figured out that the property mapped into the params list instead of a property field of it's own.

Lesson learned.

Comments

  1. That's naughty, and worse if it's not documented.

    ReplyDelete
  2. I cannot grasp the point :-)
    Can you please expose a more complete snippet?

    ReplyDelete
  3. From AnyDAC documentation:

    with ADConnection1.Params do begin
      Clear;
      Add('DriverID=Ora');
      Add('Database=ORA_920_APP');
      Add('User_Name=ADDemo');
      Add('Password=a');
    end;
    ADConnection1.Connected := True;

    ReplyDelete
  4. Fabio VITALE The issue is that the DriverName on the connection class actually sets a parameter, so when you call Params.Clear you remove the driver name as well.

    ReplyDelete
  5. Fabio VITALE - The FireDAC documentation is largely oriented towards using the components by dropping them on a form, rather than setting things up in source code.  It is possible that it is documented somewhere that the "DriverName" property is mapped into the Params list, but I failed to spot that piece of information.

    ReplyDelete
  6. They definitely need more documentation on this. Anyone with a test and production environment which I assume is almost everyone needs to change these parameters at run time.

    ReplyDelete
  7. Asbjørn Heid and Lars Fosdal:  Crystal clear! Thank you all:-)

    ReplyDelete

Post a Comment