Connect to database

Discussion of open issues, suggestions and bugs regarding MyDAC (Data Access Components for MySQL) for Delphi, C++Builder, Lazarus (and FPC)
marsheng
Posts: 62
Joined: Thu 10 May 2012 10:51

Connect to database

Post by marsheng » Fri 08 Jun 2012 06:26

I have my Delphi app all running and now I want to add a login page. Up to now, I've used the Devart login box. I think the way I connect is specific to Devart.

I tried this

Code: Select all

procedure TfLogin.bEnterClick(Sender: TObject);
begin
   dm.MyRegister.Username:=eUsername.Text;
   dm.MyRegister.Password:=ePassword.Text;
   dm.MyRegister.Connect; < ----------------- get error. 
   if dm.MyRegister.connected then
     fmain.ShowModal;
end;
Access denied. Two things, I need to know how to connect and also check if connected.

Alternatively, how do I get the source for the Connection editor ?

Thanks Wallace

AndreyZ

Re: Connect to database

Post by AndreyZ » Mon 11 Jun 2012 12:08

Hello,

When you try connecting to a MySQL server and MySQL generates an error, the MySQL error message is shown. To change this behaviour, you can write your own connection form. Please take a look at the example of such connection form in the MyDACDemo->ConnectDialog demo. The source code of the TMyConnectForm connection form is supplied with MyDAC (the MyConnectForm.pas unit), so you can change it in the way you need it. You can get more information about the "Access denied" error and the ways to avoid it here: http://dev.mysql.com/doc/refman/5.1/en/ ... enied.html

marsheng
Posts: 62
Joined: Thu 10 May 2012 10:51

Re: Connect to database

Post by marsheng » Wed 13 Jun 2012 11:07

-I'm getting TDemoFrame not found in the demo software.

-When I open Delphi, I get a SQL login screen. I want to create my own login. How do I disable the screen so I can create my own?

-What is the main difference between connecting to the sql with MyConnect and MyConnectionDialog?

Favourite saying: It is easy - if you have done it before.

AndreyZ

Re: Connect to database

Post by AndreyZ » Thu 14 Jun 2012 08:35

To solve this problem, you should open MyDACDemo, click "OK" in the error dialog, and save the project. After this, you will not have this problem.
Please take a look at the example of a custom connection dialog in the MyDACDemo->ConnectDialog demo. It demonstrates how to create a custom connection dialog and use it in the application instead of the default connection dialog.

marsheng
Posts: 62
Joined: Thu 10 May 2012 10:51

Re: Connect to database

Post by marsheng » Sun 17 Jun 2012 20:57

I seem to have lost a posting. !!!!

I've looked through the demos but can't find what I need.

How do I get the following to connect (no user input required and error trapping excluded)

Message Access denied - It will however login if I use the MyConnectdialog so my parameters are correct.

Code: Select all

procedure TForm1.Button2Click(Sender: TObject);
begin
   MyConnection1.LoginPrompt:=false;
   MyConnection1.Port:=3306;
   MyConnection1.Server:='127.0.0.1';
   MyConnection1.Username:='Root';
   MyConnection1.Password:='';
   MyConnection1.Database:='register';
   MyConnection1.Connected:=True;   <--------------  Crash
end;

marsheng
Posts: 62
Joined: Thu 10 May 2012 10:51

Re: Connect to database

Post by marsheng » Sun 17 Jun 2012 21:31

I have to apologise - My mistake, the login name is case sensitive. Root <> root.

Hopefully the last problem, What is the syntax for error handling?

Code: Select all

   try
      MyConnection1.Connected:=True;
    except
       ???
   end;
Even using MyConnectDiaglog, Delphi crashes when a parameter is incorrect.

AndreyZ

Re: Connect to database

Post by AndreyZ » Mon 18 Jun 2012 08:26

If you use a connection dialog, you can handle connection errors in the OnError event handler of the TMyConnection component. Here is a code example:

Code: Select all

procedure TMainForm.MyConnectionError(Sender: TObject; E: EDAError;
  var Fail: Boolean);
begin
  ShowMessage('Error code: ' + IntToStr(E.ErrorCode));
end;
For more information about the OnError event handler, please refer to the MyDAC documentation.

marsheng
Posts: 62
Joined: Thu 10 May 2012 10:51

Re: Connect to database

Post by marsheng » Sat 23 Jun 2012 10:16

I'm going round in circles.

MyConnection has a parameter called Connected.

How do I trap the errors when implementing it?

Code: Select all

   try
      MyConnection1.Connected:=True;
    except
       ???
   end;
The connectdialog does not give me what I want.

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: Connect to database

Post by AlexP » Sat 23 Jun 2012 12:10

Hello,

To process errors occuring when working with MyDAC, you can use the EMyError class, for example:

Code: Select all

uses ...., MyClasses;
...
  try
    MyConnection1.Connected:=True;
  except
    on E: EMyError do
      ShowMessage(Format('Error Code: %d, Error Message: %s',[e.ErrorCode, E.Message]));
  end;

marsheng
Posts: 62
Joined: Thu 10 May 2012 10:51

Re: Connect to database

Post by marsheng » Sun 24 Jun 2012 00:33

Thanks, That is what I was looking for

Code: Select all

uses ...., MyClasses;
Now it works.

marsheng
Posts: 62
Joined: Thu 10 May 2012 10:51

Re: Connect to database

Post by marsheng » Sun 24 Jun 2012 11:15

This seems to work

Code: Select all

procedure TfLogin.ConnectClick(Sender: TObject);
begin
   MyConnection1.Connected:=False;
   MyConnection1.Server:=eServer.Text;
   MyConnection1.Port:=StrToInt(ePort.Text);
   MyConnection1.Database:=eDataBase.Text;
   MyConnection1.Username:=eUsername.Text;
   MyConnection1.Password:=ePassword.Text;
   try
     MyConnection1.Connected:=True;
    except
     on E: EMyError do begin
      if e.ErrorCode = 1045 then
        ShowMessage('Invalid Server Address or Password')
       else if e.ErrorCode = 1044 then
        ShowMessage('Invalid Username or Password')
       else if e.ErrorCode = 1049 then
        ShowMessage('Trying to Connect to Unknown Database')
       else if e.ErrorCode = 2003 then
        ShowMessage('Cannot connect to server. Port Error')
       else
        ShowMessage(Format('Error Code: %d, Error Message: %s',[e.ErrorCode, E.Message]));
      end;
     else
      ShowMessage('Unknown error');
  end;
end;
I get my data from here.

Code: Select all

   Memo1.Lines.LoadFromFile('config.ini');
   eUserName.Text:=Memo1.Lines[3];
   ePassword.Text:=Memo1.Lines[4];
   eServer.Text:=Memo1.Lines[0];
   ePort.Text:=Memo1.Lines[1];
   eDataBase.Text:=Memo1.Lines[2];
Finally.

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: Connect to database

Post by AlexP » Tue 26 Jun 2012 13:04

hello,

If you have any other questions, feel free to contact us.

marsheng
Posts: 62
Joined: Thu 10 May 2012 10:51

Re: Connect to database

Post by marsheng » Tue 03 Jul 2012 11:28

All was going well until I tried to connect from outside my network.

I can connect with localhost or my fixed IP address 101.xx.xx.xx (modem loopback) from within my office network. However when connecting from outside, the package waits for quite some time and then reports a MS crash with connect to 10.0.0.16, which is my internal IP address for the SQL server????

The SQl is up and running and I can connect from outside our office (Using Heidi) to the database and table prefectly so it can't be the routers issue.

I confused where the 10.0.0.16 came from, if the Delphi app could not connect, it would have been an 101.XX.XX.XX error and one of my error messages would have been triggered.

Any suggestions ? Code above.

AndreyZ

Re: Connect to database

Post by AndreyZ » Wed 04 Jul 2012 10:03

Please make sure you are using exactly the same connection options in the TMyConnection component and Heidi. Especially check that the TMyConnection.Server property is set to the correct IP.

marsheng
Posts: 62
Joined: Thu 10 May 2012 10:51

Re: Connect to database

Post by marsheng » Wed 04 Jul 2012 10:21

They are all exactly the same. They have to be correct as I can connect locally using my routers fixed IP address.

I copied my DB to My ISP server and ran the same program. It it worked correctly.

I looked at the setting in my router for connection faults but couldn't find any.

While fiddeling around I found the Creation order form on the MyConnection box. I put MyConnection at the top of the list and it fixed it.

Why it worked localy and not on some remote systems I don't know.

Post Reply