MySQL connects queries for password even if it's in the connection string
This one looks strange.
I have a Pascal block 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;
A password prompt dialog box appears even though the password is already in the connection string. If I enter my username and password in this prompt, everything else works fine.
Here's a little weirder:
When I move the database connect command to 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 until these blocks are commented out.
When I uncomment the above units again, the problem reappears.
Some of these blocks use ADODB and DB, but I don't see how the mere presence of blocks should affect the behavior of the MySQLConnection block ....
source to share
Short answer
Set the property LoginPrompt
of the connection object False
.
Why doesn't it appear when you comment out all the form boxes?
Okay, the common DoConnect method of TCustomConnection descendants checks the property LoginPrompt
and then calls the procedural variable LoginDialogProc
/ LoginDialogExProc
if assigned. Variables are declared in Data.DB.pas
.
The VCL itself assigns these variables in the initialization
block section VCL.DBLogDlg.pas
, which contains the standard dialog you see, and this device is used by the block DBCtrls.pas
and is automatically added to your project when you use any data controlled control.
If you comment out all blocks containing information controls, the block is DBCtrls.pas
not linked in your executable and, as a consequence, there is no registered login dialog to be displayed when connected.
source to share