Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL连接提示输入密码,即使密码在连接字符串中_Mysql_Delphi_Odbc - Fatal编程技术网

MySQL连接提示输入密码,即使密码在连接字符串中

MySQL连接提示输入密码,即使密码在连接字符串中,mysql,delphi,odbc,Mysql,Delphi,Odbc,这个看起来很奇怪 我有一个连接MySQL数据库的Pascal单元 unit u_MySQLConnection; interface uses ADODB, AnsiStrings, Generics.Collections, SysUtils, DB ; type TMySQLConnection = class strict private mysqlCon : TADOConnection; public funct

这个看起来很奇怪

我有一个连接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;
我会看到一个密码提示对话框,即使密码已经在连接字符串中。如果我在这个提示符中输入用户名和密码,其他一切都正常

这里的情况有点奇怪:

当我将数据库连接命令移到main.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单元的行为

简短回答 将连接对象的
LoginPrompt
属性设置为
False

为什么在注释所有表单单元时不显示? 好的,TCustomConnection子体的公共DoConnect方法检查
LoginPrompt
属性,然后调用
LoginDialogProc
/
LoginDialogExProc
过程变量(如果已赋值)。变量在
Data.DB.pas
中声明

VCL本身在
VCL.DBLogDlg.pas
单元的
initialization
部分分配此变量,该单元包含您看到的标准对话框,该单元由
DBCtrls.pas
单元使用,并在使用任何数据感知控件时自动添加到项目中


如果注释掉包含数据感知控件的所有单元,则可执行文件中不会链接
DBCtrls.pas
单元,因此,在连接时不会显示注册的登录对话框。

尝试将'mysqlCon'的
LoginPrompt
设置为false?之后:mysqlCon:=TADOConnection.Create(nil);添加mysqlcon.LoginPromt:=false;密码的ODBC属性是PWD而不是password,尽管mysql驱动程序可能也理解password。+1特别是用于解释为什么在应用程序中没有任何表单存在/处于活动状态时不会出现提示。