Connector/Net Tutorials
1857
20.2.4.1.1. The MySqlConnection Object
For your Connector/Net application to connect to a MySQL database, it must establish a connection by
using a
MySqlConnection
object.
The
MySqlConnection
constructor takes a connection string as one of its parameters. The
connection string provides necessary information to make the connection to the MySQL database. The
connection string is discussed more fully in
Section 20.2.5.1, “Connecting to MySQL Using Connector/
Net”
. For a list of supported connection string options, see
Section 20.2.6, “Connector/Net Connection
String Options Reference”
.
The following code shows how to create a connection object:
using System;
using System.Data;
using MySql.Data;
using MySql.Data.MySqlClient;
public class Tutorial1
{
public static void Main()
{
string connStr = "server=localhost;user=root;database=world;port=3306;password=******;";
MySqlConnection conn = new MySqlConnection(connStr);
try
{
Console.WriteLine("Connecting to MySQL...");
conn.Open();
// Perform database operations
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
conn.Close();
Console.WriteLine("Done.");
}
}
When the
MySqlConnection
constructor is invoked, it returns a connection object, which is used for
subsequent database operations. Open the connection before any other operations take place. Before
the application exits, close the connection to the database by calling
Close
on the connection object.
Sometimes an attempt to perform an
Open
on a connection object can fail, generating an exception
that can be handled using standard exception handling code.
In this section you have learned how to create a connection to a MySQL database, and open and close
the corresponding connection object.
20.2.4.1.2. The MySqlCommand Object
Once a connection has been established with the MySQL database, the next step is do carry out the
desired database operations. This can be achieved through the use of the
MySqlCommand
object.
You will see how to create a
MySqlCommand
object. Once it has been created, there are three main
methods of interest that you can call:
• ExecuteReader - used to query the database. Results are usually returned in a
MySqlDataReader
object, created by
ExecuteReader
.
• ExecuteNonQuery - used to insert and delete data.
• ExecuteScalar - used to return a single value.
Once a
MySqlCommand
object has been created, you will call one of the above methods on it to
carry out a database operation, such as perform a query. The results are usually returned into a
Содержание 5.0
Страница 1: ...MySQL 5 0 Reference Manual ...
Страница 18: ...xviii ...
Страница 60: ...40 ...
Страница 396: ...376 ...
Страница 578: ...558 ...
Страница 636: ...616 ...
Страница 844: ...824 ...
Страница 1234: ...1214 ...
Страница 1426: ...MySQL Proxy Scripting 1406 The following diagram shows an overview of the classes exposed by MySQL Proxy ...
Страница 1427: ...MySQL Proxy Scripting 1407 ...
Страница 1734: ...1714 ...
Страница 1752: ...1732 ...
Страница 1783: ...Configuring Connector ODBC 1763 ...
Страница 1793: ...Connector ODBC Examples 1773 ...
Страница 1839: ...Connector Net Installation 1819 2 You must choose the type of installation to perform ...
Страница 1842: ...Connector Net Installation 1822 5 Once the installation has been completed click Finish to exit the installer ...
Страница 1864: ...Connector Net Visual Studio Integration 1844 Figure 20 24 Debug Stepping Figure 20 25 Function Stepping 1 of 2 ...
Страница 2850: ...2830 ...
Страница 2854: ...2834 ...
Страница 2928: ...2908 ...
Страница 3000: ...2980 ...
Страница 3122: ...3102 ...
Страница 3126: ...3106 ...
Страница 3174: ...3154 ...
Страница 3232: ...3212 ...