How to prevent ''Access denied for user''

Discussion of open issues, suggestions and bugs regarding MyDAC (Data Access Components for MySQL) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
mierlp
Posts: 29
Joined: Thu 26 Jan 2006 08:34
Location: Nederlands

How to prevent ''Access denied for user''

Post by mierlp » Thu 09 Oct 2008 11:29

hi,

i use a secure .ini file which contains the settings for connecting to
a database (user, password, database, port and server name (IP nr).

Within the OnCreate event from a datamodule i use this settings to make the connection. When some of the settings are wrong (wrong password)
the user gets the default (i think MYSQL) message "'Access denied
for user ....''.

Is it possible to disable this message and use something like :

if not MyConnection.Connected then ....
message.....
else
......

I did try this but can't get is working....within the datamodule OnCreate event i use the following code to connect :

Code: Select all

procedure TdmTables.DataModuleCreate(Sender: TObject);
var
  IniFile : TIniFile;
  i : integer;
begin
    cMapProg:= ExtractFilePath(Application.ExeName);
    if not fileExists(cMapProg+'database.dat') then begin
       Application.MessageBox('Settings file does not exist !'+#10+''+#10+'The applications will be terminated', 'Warning !', MB_OK+MB_ICONHAND+MB_DEFBUTTON1+MB_APPLMODAL);
       Application.Terminate;
    end;
    if fileExists(cMapProg+'database.dat') then begin
       // Call .ini file to read settings
       GetSetting;
       // variabelen toekennen
       dmTables.MyConnection.Server:=cRemoteserver;
       dmTables.MyConnection.Database:=cRemoteDB;
       dmTables.MyConnection.Username:=cRemoteUser;
       dmTables.MyConnection.Password:=cRemotePassword;
       dmTables.MyConnection.Port:=strTOint(cRemotePort);

       dmTables.MyConnection.Connected:=true;
       if not dmTables.MyConnection.Connected then begin
          MessageDlg('Warning' + #10 + '' + #10 + 'It's not possible to maken connection with database.' + #10 + 'Application will be terminated !', mtError, [mbOK], 0);
          Application.Terminate;
       end;
       if dmTables.MyConnection.Connected then begin
          for i := 0 to dmTables.ComponentCount -1 do begin
             if Components[i] is TMyQuery then begin
               (Components[i] as TMyQuery).Active := true;
             end;
          end;
       end;
   end;
end;

cybsistemas
Posts: 118
Joined: Mon 12 Sep 2005 17:31
Location: Argentina

Post by cybsistemas » Thu 09 Oct 2008 12:00

in the event onError de myconnection
procedure MyconnectionError(Sender: TObject; E: EDAError; var Fail: Boolean);
begin


If e.IsFatalError then
Begin
ShowMessage ('xxxxxxxxxxxxxxxx' + IntToStr(e.ErrorCode) );
Application.Terminate;
End

Else If e.IsKeyViolation then
Begin
ShowMessage ('Duplicate key'+IntToStr(e.ErrorCode));
End

Else
ShowMessage (trim (e.Message));
Fail := False;
End;

mierlp
Posts: 29
Joined: Thu 26 Jan 2006 08:34
Location: Nederlands

Post by mierlp » Thu 09 Oct 2008 13:20

Hi

thanks for the reply...i did the following based on your reply.
The error code which exist if ...lets say a wrong password is used
is the following 28000 Access denied for

Internet manual says:
Error: 1045 SQLSTATE: 28000 (ER_ACCESS_DENIED_ERROR)
Message: Access denied for user '%s'@'%s' (using password: %s)

Now i use the following code below BUT this does not prevent
showing many times the mysql error message

What's a good solution for creating a connection using MyConnection
in combination with the BeforeConnect or DataModule OnCreateEVent

Code: Select all

procedure TdmTables.MyConnectionError(Sender: TObject; E: EDAError;
  var Fail: Boolean);
begin
  If (E is EMyError) then begin
   if (EMyError(E).ErrorCode=ER_ACCESS_DENIED_ERROR) then begin
      MessageDlg('Waarschuwing' + #10 + '' + #10 + 'Er kan GEEN verbinding worden gemaakt met de database.' + #10 + 'Controleer de instellingen van de applicatie middels SETDB.EXE' + #10 + 'en of de gedefinieerde gebruiker voldoende rechten heeft.' + #10 + '' + #10 + 'Applicatie wordt nu automatische beëindigd  !', mtError, [mbOK], 0);
      Application.Terminate;
   end;
  end;
end;

cybsistemas
Posts: 118
Joined: Mon 12 Sep 2005 17:31
Location: Argentina

Post by cybsistemas » Thu 09 Oct 2008 13:35

procedure TdmTables.MyConnectionError(Sender: TObject; E: EDAError;
var Fail: Boolean);
begin

If (E is EMyError) then begin
if (EMyError(E).ErrorCode=ER_ACCESS_DENIED_ERROR) then begin
MessageDlg('Waarschuwing' + #10 + '' + #10 + 'Er kan GEEN verbinding worden gemaakt met de database.' + #10 + 'Controleer de instellingen van de applicatie middels SETDB.EXE' + #10 + 'en of de gedefinieerde gebruiker voldoende rechten heeft.' + #10 + '' + #10 + 'Applicatie wordt nu automatische beëindigd !', mtError, [mbOK], 0);
Fail := False;
Application.Terminate;
end;
end;
end;

Post Reply