这个看起来很奇怪。
我有一个连接到 MySQL 数据库的 Pascal 单元
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.
当我尝试连接时
MySQLConnection := TMySQLConnection.Create;
try
MySQLConnection.Connect;
finally
MySQLConnection.Destroy;
end;
即使密码已经在连接字符串中,也会出现密码提示对话框。如果我在此提示中输入用户名和密码,其他一切正常。
这里的事情变得有点奇怪:
当我将数据库连接命令移动到主 .dpr 文件中时,如图所示
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;
那么密码提示就不会出现了,只要把那些单位注释掉就行了。
当我再次取消注释上述单位时,问题再次出现。
其中一些单元确实使用 ADODB 和 DB,但我看不出这些单元的存在应该如何影响 MySQLConnection 单元的行为......