Oracle 5.0 Reference Manual page 1877

Table of Contents

Advertisement

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
The
MySqlConnection
connection string provides necessary information to make the connection to the MySQL database. The
connection string is discussed more fully in
Net". For a list of supported connection string options, see
String Options
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
{
}
catch (Exception ex)
{
}
conn.Close();
Console.WriteLine("Done.");
}
}
When the
MySqlConnection
subsequent database operations. Open the connection before any other operations take place. Before
the application exits, close the connection to the database by calling
Sometimes an attempt to perform an
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
You will see how to create a
methods of interest that you can call:
• ExecuteReader - used to query the database. Results are usually returned in a
object, created by ExecuteReader.
• ExecuteNonQuery - used to insert and delete data.
• ExecuteScalar - used to return a single value.
Once a
MySqlCommand
carry out a database operation, such as perform a query. The results are usually returned into a
Connector/Net Tutorials
object.
constructor takes a connection string as one of its parameters. The
Reference".
Console.WriteLine("Connecting to MySQL...");
conn.Open();
// Perform database operations
Console.WriteLine(ex.ToString());
constructor is invoked, it returns a connection object, which is used for
MySqlCommand
object has been created, you will call one of the above methods on it to
Section 20.2.5.1, "Connecting to MySQL Using Connector/
Section 20.2.6, "Connector/Net Connection
on a connection object can fail, generating an exception
Open
object. Once it has been created, there are three main
1857
on the connection object.
Close
object.
MySqlCommand
MySqlDataReader

Advertisement

Table of Contents
loading

This manual is also suitable for:

Mysql 5.0

Table of Contents