MyDAC

Connecting to MySQL Embedded

This tutorial describes how to connect to MySQL Embedded.

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 Embedded Server, you need MySQL Embedded Server files, MyDAC installed, and IDE running. Also, if authentication is enabled in the settings of MySQL Embedded Server, you need to know the login and password (by default, authentication is disabled).

General information

Components for connection to MySQL Embedded Server

It is possible to connect to MySQL Embedded Server using both TMyEmbConnection and TMyConnection components. But, in contrast to TMyConnection, TMyEmbConnection doesn't require my.ini and allows to set connection with MySQL Embedded Server via the TMyEmbConnection.Params, TMyEmbConnection.BaseDir and TMyEmbConnection.DataDir properties.

Installation of MySQL Embedded Server

Creating Connection

Design time creation

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

TMyEmbConnection:

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

TMyConnection:

  1. Open the Component palette and find the TMyConnection component in the MyDAC category.
  2. Double-click on the component. Note that a 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 MyEmbConnection1 or MyConnection1 component. You can do this in two ways:

Using Connection Editor

TMyEmbConnection:

  1. Double-click on the created connection object (MyEmbConnection1 or MyConnection1).
  2. For the MyConnection1 object enable the Embedded check box.
  3. In the Database edit box specify the database name (for example, test). If the specified database does not exist, you will get an error on connection.
  4. If authentication is enabled, specify the user name in the Username edit box and the password in the Password edit box.

Using Object Inspector

TMyEmbConnection:

  1. Click on the created connection object (MyEmbConnection1 or MyConnection1) and press F11 to focus on the object properties.
  2. For MyConnection1 object set Options.Embedded to True.
  3. In the Database edit box specify the database name (for example, test). If the specified database does not exist, you will get an error on connection.
  4. If authentication is enabled, specify the user name in the Username property and the password in the Password property.

Run time creation

The same operations performed in runtime look as follows:

TMyEmbConnection:

[Delphi]

procedure TMainForm.ButtonConnectClick(Sender: TObject);
var
  con: TMyEmbConnection;
begin
  con := TMyEmbConnection.Create(nil);
  try
    con.Database := 'test'; // if the specified database does not exist, you will get an error on connecting
    con.Password := 'password'; // if authentication is enabled
    con.Username := 'username'; // if authentication is enabled
    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 MSCompactConnection and OLEDBAccess units to the USES clause of your unit.


[C++Builder]


void __fastcall TMainForm::ButtonConnectClick(TObject *Sender)
{
  TMyEmbConnection* con = new TMyEmbConnection(NULL);
  try
  {
    con->Database = "test"; // if the specified database does not exist, you will get an error on connecting
    con->Password = "password"; // if authentication is enabled
    con->Username = "username"; // if authentication is enabled
    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 MyEmbConnection.hpp header file to your unit.

TMyConnection:


[Delphi]


procedure TMainForm.ButtonConnectClick(Sender: TObject);
var
  con: TMyConnection;
begin
  con := TMyConnection.Create(nil);
  try
    con.Options.Embedded := True;  // enable embedded mode

    con.Database := 'test'; // if the specified database does not exist, you will get an error on connecting
    con.Password := 'password'; // if authentication is enabled
    con.Username := 'username'; // if authentication is enabled
    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 unit to the USES clause of your unit.


[C++ Builder]


void __fastcall TMainForm::ButtonConnectClick(TObject *Sender)
{
  TMyConnection* con = new TMyConnection(NULL);
  try
  {
    con->Options->Embedded = True;  // enable embedded mode

    con->Database = "database"; // if the specified database does not exist, you will get an error on connecting
    con->Password = "password"; // if authentication is enabled
    con->Username = "username"; // if authentication is enabled
    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 MSAccess.hpp header file to your unit.

Opening connection

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

[Delphi]


con.Open;

[C++ Builder]


con->Open();

Another way to open 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, the connection (TMyEmbConnection or TMyConnection) must have valid connection options assigned earlier. When you call Open, MyDAC tries to open the database. If any problem occurs, it raises an exception with brief explanation on what is wrong. If no problem is encountered and the database is opened, the Open method returns and the Connected property is changed to True.

Closing connection

To close 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 the properties of the TMyEmbConnection or TMyConnection component. Keep in mind that while some of the properties can be altered freely, most of them close connection when a new value is assigned. For example, if you change the Username property, it is closed immediately, and you have to reopen it manually.

Additional information

To connect to MySQL Embedded Server versions 5.6.x, it is necessary to set the MySQLEmbDisableEventLog global variable (declared in the MySqlApi module) to True before opening the connection. You can read additional information about connecting to MySQL Embedded Server in the Embedded Server article.

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-2024 Devart. All Rights Reserved. Request Support DAC Forum Provide Feedback