This tutorial describes how to connect to MySQL using the TMyConnection component.
This tutorial assumes that you have installed MyDAC and run the database server and the IDE. You need to know the server address, the port number (if you use a port other than the default port 3306
), the database name, and the username and password. To connect at runtime, add the MyAccess unit to the uses
clause for Delphi or include the MyAccess.hpp
header file for C++ Builder.
To establish a connection to the server, set up the properties of the TMyConnection component: Server, Port, Database, Username, and Password. You can also specify all connection parameters in the ConnectString property.
You can connect to MySQL in the Direct
mode or using the MySQL client library. The connection mode is defined by the Direct property, which defaults to True
. To access MySQL using the client library, set the property to False
.
MyDAC can also work with the embedded MySQL server. For information about connecting to the embedded server, see Connecting to MySQL Embedded.
The following assumes that you have already created or opened an existing form in the IDE. At design-time, you can set up a TMyConnection
object in the TMyConnection Editor or Object Inspector.
TMyConnection
component in the MyDAC
category on the Tool Palette.
TMyConnection
object in this project, the object will be named MyConnection1
.
Using TMyConnection Editor
MyConnection1
object.Server
edit box.3306
, specify it in the Port
edit box.Database
edit box.root
by default) in the Username
edit box.Password
edit box.Using Object Inspector
MyConnection1
object on the form.Database
property to the database name.Password
property to the password (root
by default).3306
, set the Port
property to your port.Server
property to the DNS name or IP address of the MySQL server.Username
property to the username (empty value by default).The same connection parameters at runtime are set up as follows:
Delphi
var
MyConnection1: TMyConnection;
begin
MyConnection1 := TMyConnection.Create(nil);
try
// adds connection parameters
MyConnection1.Server := 'server';
MyConnection1.Database := 'database';
MyConnection1.Username := 'username';
MyConnection1.Password := 'password';
MyConnection1.Port := 3306;
// disables a login prompt
MyConnection1.LoginPrompt := False;
// opens a connection
MyConnection1.Open;
finally
MyConnection1.Free;
end;
end;
C++ Builder
TMyConnection* MyConnection1 = new TMyConnection(NULL);
try {
// adds connection parameters
MyConnection1->Server = "server";
MyConnection1->Database = "database";
MyConnection1->Username = "username";
MyConnection1->Password = "password";
MyConnection1->Port = 3306;
// disables a login prompt
MyConnection1->LoginPrompt = False;
// opens a connection
MyConnection1->Open();
}
__finally {
MyConnection1->Free();
}
To open a connection at run-time, call the Open
method:
Delphi
MyConnection1.Open;
C++Builder
MyConnection1->Open;
Another way to open a connection at runtime is to set the Connected property to True
:
Delphi
MyConnection1.Connected := True;
C++ Builder
MyConnection1->Connected = True;
You can also set up the Connected
property at design-time in the Object Inspector.
You can modify a connection by changing properties of the TMyConnection
object. Note that while some of the object's properties can be altered without changing the state of a connection, in most cases, a connection is closed when a new value is assigned to the property. For example, if you change the value of the Server
property, a connection is closed immediately and you need to reopen it manually.
To close a connection, call the Close
method or set the Connected
property to False
:
Delphi
MyConnection1.Close;
or:
MyConnection1.Connected := False;
C++ Builder
MyConnection1->Close;
or:
MyConnection1->Connected = False;
MyDAC offers a wide set of features to achieve better performance, balance network load, and enable additional capabilities, for example: