MyDAC

Logging on to MySQL Server

This tutorial describes how to connect to MySQL.

Contents

  1. Requirements
  2. General information
  3. Creating Connection
  4. Opening connection
  5. Closing connection
  6. Modifying connection
  7. Additional information
  8. See Also

Requirements

In order to connect to MySQL server, you need the server itself running, MyDAC installed, and the IDE running. Also, you need to know the following information:

General information

To establish connection to the server, you have to provide some connection parameters to MyDAC. This information is used by the TMyConnection component to establish connection to the server. The parameters are represented by the properties of the TMyConnection component (Server, Port, Database, Username, Password).

There are two ways to connect to MySQL: using the Direct mode and using MySQL client library. This is controlled by the TMyConnectionOptions.Direct property. By default, the Direct property is set to True, which means that the Direct mode is used for connection to the server. If the Direct property is set to False, MyDAC uses MySQL client library for connection to the server.

MyDAC also can work with Embedded MySQL server. For more information about connection to Embedded MySQL server, please refer to the Connecting To Embedded MySQL server topic.

Creating Connection

Design time creation

The following assumes that you have the IDE running, and you are currently focused on the form designer.

  1. Open the Component palette and find the TMyConnection component in the MyDAC category.
  2. Double-click on the component. Note that the new object appears on the form. If this is the first time you create TMyConnection in this application, it is named MyConnection1.

After you have done these steps, you should set up the newly created MyConnection1 component. You can do this in two ways:

Using TMyConnection Editor

Double-click on the MyConnection1 object.

  1. In the Server edit box specify a DNS name or IP address of the computer, where MySQL resides;
  2. In the Port edit box specify the port that the MySQL server listens to;
  3. In the Username edit box specify a login;
  4. In the Password edit box specify a password. If a login has no password, leave the Password edit box blank;
  5. In the Database edit box specify the database name.

Using Object Inspector

  1. Click on the MyConnection1 object and press F11 to focus on object's properties;
  2. Set the Server property to a DNS name or IP address of the computer, where MySQL server resides;
  3. Set the Port property to a port that the MySQL server listens to.
  4. Set a login in the Username property;
  5. Set a password in the Password property. If a login does not have a password, leave the Password property blank.
  6. In the Database property specify the database name.

Run time creation

The same operations performed in runtime look as follows:
[Delphi]


var
  con: TMyConnection;
begin
  con := TMyConnection.Create(nil);
  try
    con.Server := 'server';
    con.Port := 3306;
    con.Username := 'username';
    con.Password := 'password';
    con.Database := 'database';
    con.LoginPrompt := False; // to prevent showing of the connection dialog
    con.Open;
  finally
    con.Free;
  end;
end;

Note: To run this code, you have to add the MSAccess and OLEDBAccess units to the USES clause of your unit.


[C++Builder]


{
  TMyConnection* con = new TMyConnection(NULL);
  try
  {
    con->Server = "server";
    con->Port = 3306;
    con->Username = "username";
    con->Password = "password";
    con->Database = "database";
    con->LoginPrompt = False; // to prevent showing of the connection dialog
    con->Open();
  }
  __finally
  {
    con->Free();
  }
}

Note: To run this code, you have to include the MyAccess.hpp header file to your unit.

Opening connection

As you can see above, opening a connection at run-time is as simple as calling of the Open method:

[Delphi]


con.Open;

[C++ Builder]


con->Open();

Another way to open a connection at run-time is to set the Connected property to True:

[Delphi]


con.Connected := True;

[C++ Builder]


con->Connected = True;

This way can be used at design-time as well. Of course, MyConnection1 must have valid connection options assigned earlier. When you call Open, MyDAC tries to find the host and connect to the server. If any problem occurs, it raises an exception with brief explanation on what is wrong. If no problem is encountered, MyDAC tries to establish connection. Finally, when connection is established, the Open method returns and the Connected property is changed to True.

Closing connection

To close a connection, call its Close method, or set its Connected property to False:
[Delphi]


con.Close;

[C++ Builder]


con.Close();

or:

[Delphi]


con.Connected := False;

[C++ Builder]


con.Connected = False;

Modifying connection

You can modify connection by changing properties of the TMyConnection object. Keep in mind that while some of the properties can be altered freely, most of them close connection when the a value is assigned. For example, if you change Server property, it is closed immediately, and you have to reopen it manually.

Additional information

MyDAC has a wide set of features you can take advantage of. The following list enumerates some of them, so you can explore the advanced techniques to achieve better performance, balance network load or enable additional capabilities:

See Also

© 1997-2017 Devart. All Rights Reserved. Request Support DAC Forum Provide Feedback