MySQL connection prompts for password even though this is in the connection string

delphi, mysql, odbc

Solution

Short answer

Set the `LoginPrompt` property of the connection object to `False`.

Why it does not appear when you comment all the form units?

Well, the common DoConnect method of the TCustomConnection descendants checks the `LoginPrompt` property and then calls the `LoginDialogProc`/`LoginDialogExProc` procedural variable if assigned. The variables are declared in `Data.DB.pas`.

The VCL itself assigns this variables in the `initialization` section of the `VCL.DBLogDlg.pas` unit, which contains the standard dialog box you see, and this unit is used by the `DBCtrls.pas` unit, and automatically added to your project when you use any data-aware control.

If you comment out all the units containing data aware controls, the `DBCtrls.pas` unit is not linked in your executable and, as a consequence there's no registered login dialog to show when you're connecting.

Problem

This one looks like a bizarre one. I have a Pascal unit that connects to a MySQL database ``` unit u_MySQLConnection; interface uses ADODB, AnsiStrings, Generics.Collections, SysUtils, DB ; type TMySQLConnection = class strict private mysqlCon : TADOConnection; public function Connect:boolean; destructor Destroy; end; var MySQLConnection : TMySQLConnection; implementation function TMySQLConnection.Connect:boolean; var success : boolean; begin success := true; try if NOT (mysqlCon = nil) then mysqlCon.Destroy; mysqlCon := TADOConnection.Create(nil); mysqlCon.ConnectionString := 'DRIVER={MySQL ODBC 3.51 Driver}; SERVER=localhost; DATABASE=database; UID=root; PASSWORD=password;OPTION=3;'; except success := false; end; Result := success; end; destructor TMySQLConnection.Destroy; begin FreeAndNil(mysqlCon); inherited; end; end. ``` And when I try to connect ``` MySQLConnection := TMySQLConnection.Create; try MySQLConnection.Connect; finally MySQLConnection.Destroy; end; ``` I get a password prompt dialog box appearing, even though the password is already in the connection string. If I enter the username and password into this prompt, everything else works fine. Things get a little bit more strange here: When i move the database connection command into the main .dpr file as shown ``` program DieselBatch; uses Vcl.Forms, u_MySQLConnection in '..\src\u_MySQLConnection.pas' (*, frm_About in '..\src\frm_About.pas' {frmAbout}, frm_AnalystDetails in '..\src\frm_AnalystDetails.pas' {frmAnalystDetails}, frm_Batch in '..\src\frm_Batch.pas' {frmBatch}, frm_ConfirmResultsChanged in '..\src\frm_ConfirmResultsChanged.pas' {frmConfirmResultsChanged}, frm_DebugSample in '..\src\frm_DebugSample.pas' {frmDebugSample}, frm_FlashManualEntry in '..\src\frm_FlashManualEntry.pas' {frmFlashEntry}, frm_Main in '..\src\frm_Main.pas' {frmMain}, frm_SampleComment in '..\src\frm_SampleComment.pas' {frmSampleComment}, frm_SelectAnalystForResult in '..\src\frm_SelectAnalystForResult.pas' {frmSelectAnalystForResult}, u_Data in '..\src\u_Data.pas', u_MicroCheck in '..\src\u_MicroCheck.pas', u_Undo in '..\src\u_Undo.pas' *) ; {$R *.res} var MySQLConnection : TMySQLConnection; begin MySQLConnection := TMySQLConnection.Create; try MySQLConnection.Connect; finally MySQLConnection.Destroy; end; ``` Then the password prompt does not appear, as long as those units are commented out. When I uncomment the above units again, the problem reappears. Some of those units do use ADODB and DB but I can't see how the mere presence of the units should affect the behavour of the MySQLConnection unit ....

Original source