You are on page 1of 37

MySQL CSharp tutorial

http://zetcode.com/db/mysqlcsharptutorial/
About this tutorial
This is a C# tutorial for the MySQL database. It covers the basics of MySQL programming
with C#. In this tutorial, we use the Connector/Net driver. This driver is based on the
ADO.NET specification. The examples were created and tested on Ubuntu Linux. There is
a similar MySQL Visual Basic tutorial on ZetCode.

If you need to refresh your knowledge of the C# language, there is a full C# tutorial on
ZetCode.

About MySQL database


MySQL is a leading open source database management system. It is a multi user,
multithreaded database management system. MySQL is especially popular on the web. It is
one part of the very popular LAMP platform consisting of Linux, Apache, MySQL, and
PHP. Currently MySQL is owned by Oracle. MySQL database is available on most
important OS platforms. It runs on BSD Unix, Linux, Windows or Mac OS. Wikipedia and
YouTube use MySQL. These sites manage millions of queries each day. MySQL comes in
two versions: MySQL server system and MySQL embedded system.

Before we start
On Linux, we need to install several packages to execute the examples in this tutorial:
libmysql6.1-cil, mysql-server, mysql-client. We also need to install C# compiler
from the Mono project, either from a package or from sources.

The libmysql6.1-cil is the MySQL database connector for CLI. It is written in C# and is
available for all CLI languages: C#, Visual Basic, Boo, and others.

$ ls /usr/lib/cli/MySql.Data-6.1/MySql.Data.dll
/usr/lib/cli/MySql.Data-6.1/MySql.Data.dll

From the technical point of view, we need a DLL. On an Ubuntu Linux, it was located
under the above path. We need to know the path to the DLL library. To compile our
examples.

If you do not already have MySQL installed, we must install it.

$ sudo apt-get install mysql-server


This command installs the MySQL server and various other packages. While installing the
package, we are prompted to enter a password for the MySQL root account. For installing
MySQL from sources, have a look at MySQL installation page.

$ service mysql status


mysql start/running, process 1238

We check if the MySQL server is running. If not, we need to start the server.

$ sudo -b /usr/local/mysql/bin/mysqld_safe

The above command starts MySQL server using the MySQL server startup script. The way
how we start a MySQL server might be different. It depends whether we have installed
MySQL from sources or from packages and also on the Linux distro. For further
information consult MySQL first steps or your Linux distro information.

Next, we are going to create a new database user and a new database. We use the mysql
client.

$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 30
Server version: 5.0.67-0ubuntu6 (Ubuntu)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> SHOW DATABASES;


+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
+--------------------+
2 rows in set (0.00 sec)

We use the mysql monitor client application to connect to the server. We connect to the
database using the root account. We show all available databases with the SHOW DATABASES
statement.

mysql> CREATE DATABASE mydb;


Query OK, 1 row affected (0.02 sec)

We create a new mydb database. We will use this database throughout the tutorial.

mysql> CREATE USER user12@localhost IDENTIFIED BY '34klq*';


Query OK, 0 rows affected (0.00 sec)

mysql> USE mydb;


Database changed
mysql> GRANT ALL ON mydb.* to user12@localhost;
Query OK, 0 rows affected (0.00 sec)

mysql> quit;
Bye

We create a new database user. We grant all privileges to this user for all tables of the mydb
database.

Definitions
ADO.NET is an important part of the .NET framework. It is a specification that unifies
access to relational databases, XML files and other application data. A MySQL
Connector/Net is an implementation of the ADO.NET specification for the MySQL
database. It is a driver written in C# language and is available for all .NET languages.

The Connection, Command, DataReader, DataSet, and DataProvider are the core
elements of the .NET data provider model. The Connection creates a connection to a
specific data source. The Command object executes an SQL statement against a data source.
The DataReader reads streams of data from a data source. The DataSet object is used for
offline work with a mass of data. It is a disconnected data representation that can hold data
from a variety of different sources. Both DataReader and DataSet are used to work with
data; they are used under different circumstances. If we only need to read the results of a
query, the DataReader is the better choice. If we need more extensive processing of data,
or we want to bind a Winforms control to a database table, the DataSet is preferred.

MySQL version
If the following program runs OK, then we have everything installed OK. We check the
version of the MySQL server.

using System;
using MySql.Data.MySqlClient;

public class Example


{

static void Main()


{
string cs = @"server=localhost;userid=user12;
password=34klq*;database=mydb";

MySqlConnection conn = null;

try
{
conn = new MySqlConnection(cs);
conn.Open();
Console.WriteLine("MySQL version : {0}", conn.ServerVersion);
} catch (MySqlException ex)
{
Console.WriteLine("Error: {0}", ex.ToString());

} finally
{
if (conn != null)
{
conn.Close();
}
}
}
}

We connect to the database and get some info about the MySQL server.

using MySql.Data.MySqlClient;

We import the elements of the MySQL data provider.

string cs = @"server=localhost;userid=user12;
password=34klq*;database=mydb";

This is the connection string. It is used by the data provider to establish a connection to the
database. We specify the host name, user name, password and a database name.

conn = new MySqlConnection(cs);

A MySQLConnection object is created. This object is used to open a connection to a


database.

conn.Open();

This line opens the database connection.

Console.WriteLine("MySQL version : {0}", conn.ServerVersion);

Here we print the version of MySQL using the ServerVersion property of the connection
object.

} catch (MySqlException ex)


{
Console.WriteLine("Error: {0}", ex.ToString());

In case of an exception, we print the error message to the console.

} finally
{
if (conn != null)
{
conn.Close();
}
}

At the final step, we close the connection object.

$ dmcs -r:/usr/lib/cli/MySql.Data-6.1/MySql.Data.dll version.cs

We compile our example. A path to the MySQL connector DLL is provided.

$ ./version.exe
MySQL version : 5.5.9

This is the output of the program on my system.

A more complex program follows.

using System;
using MySql.Data.MySqlClient;

public class Example


{

static void Main()


{
string cs = @"server=localhost;userid=user12;
password=34klq*;database=mydb";

MySqlConnection conn = null;

try
{
conn = new MySqlConnection(cs);
conn.Open();

string stm = "SELECT VERSION()";


MySqlCommand cmd = new MySqlCommand(stm, conn);
string version = Convert.ToString(cmd.ExecuteScalar());
Console.WriteLine("MySQL version : {0}", version);

} catch (MySqlException ex)


{
Console.WriteLine("Error: {0}", ex.ToString());

} finally
{

if (conn != null)
{
conn.Close();
}

}
}
}
We check for the version of the MySQL database. This time using an SQL query.

string stm = "SELECT VERSION()";

This is the SQL SELECT statement. It returns the version of the database. The VERSION()
is a built-in MySQL function.

MySqlCommand cmd = new MySqlCommand(stm, conn);

The MySqlCommand is an object, which is used to execute a query on the database. The
parameters are the SQL statement and the connection object.

string version = Convert.ToString(cmd.ExecuteScalar());

There are queries which return only a scalar value. In our case, we want a simple string
specifying the version of the database. The ExecuteScalar() is used in such situations.
We avoid the overhead of using more complex objects.

$ ./version2.exe
MySQL version : 5.5.9

Same result as in the previous example.

Creating and populating tables


Next we are going to create database tables and fill them with data. These tables will be
used throughout this tutorial.

DROP TABLE IF EXISTS Books, Authors;

CREATE TABLE IF NOT EXISTS Authors(Id INT PRIMARY KEY AUTO_INCREMENT,


Name VARCHAR(25)) ENGINE=INNODB;

INSERT INTO Authors(Id, Name) VALUES(1, 'Jack London');


INSERT INTO Authors(Id, Name) VALUES(2, 'Honore de Balzac');
INSERT INTO Authors(Id, Name) VALUES(3, 'Lion Feuchtwanger');
INSERT INTO Authors(Id, Name) VALUES(4, 'Emile Zola');
INSERT INTO Authors(Id, Name) VALUES(5, 'Truman Capote');

CREATE TABLE IF NOT EXISTS Books(Id INT PRIMARY KEY AUTO_INCREMENT,


AuthorId INT, Title VARCHAR(100),
FOREIGN KEY(AuthorId) REFERENCES Authors(Id) ON DELETE CASCADE)
ENGINE=INNODB;

INSERT INTO Books(Id, AuthorId, Title) VALUES(1, 1, 'Call of the Wild');


INSERT INTO Books(Id, AuthorId, Title) VALUES(2, 1, 'Martin Eden');
INSERT INTO Books(Id, AuthorId, Title) VALUES(3, 2, 'Old Goriot');
INSERT INTO Books(Id, AuthorId, Title) VALUES(4, 2, 'Cousin Bette');
INSERT INTO Books(Id, AuthorId, Title) VALUES(5, 3, 'Jew Suess');
INSERT INTO Books(Id, AuthorId, Title) VALUES(6, 4, 'Nana');
INSERT INTO Books(Id, AuthorId, Title) VALUES(7, 4, 'The Belly of
Paris');
INSERT INTO Books(Id, AuthorId, Title) VALUES(8, 5, 'In Cold blood');
INSERT INTO Books(Id, AuthorId, Title) VALUES(9, 5, 'Breakfast at
Tiffany');

We have a books.sql file. It creates two database tables: Authors and Books. The tables
are of InnoDB type. InnoDB databases support foreign key constraints and transactions. We
place a foreign key constraint on the AuthorId column of the Books table. We fill the
tables with initial data.

mysql> source books.sql


Query OK, 0 rows affected (0.07 sec)
Query OK, 0 rows affected (0.12 sec)
Query OK, 1 row affected (0.04 sec)
...

We use the source command to execute the books.sql script.

Prepared statements
Now we will concern ourselves with prepared statements. When we write prepared
statements, we use placeholders instead of directly writing the values into the statements.
Prepared statements increase security and performance.

using System;
using MySql.Data.MySqlClient;

public class Example


{

static void Main()


{
string cs = @"server=localhost;userid=user12;
password=34klq*;database=mydb";

MySqlConnection conn = null;

try
{
conn = new MySqlConnection(cs);
conn.Open();

MySqlCommand cmd = new MySqlCommand();


cmd.Connection = conn;
cmd.CommandText = "INSERT INTO Authors(Name) VALUES(@Name)";
cmd.Prepare();

cmd.Parameters.AddWithValue("@Name", "Trygve Gulbranssen");


cmd.ExecuteNonQuery();

} catch (MySqlException ex)


{
Console.WriteLine("Error: {0}", ex.ToString());

} finally
{
if (conn != null) {
conn.Close();
}

}
}
}

We add a new author to the Authors table. We use a parameterized command.

cmd.CommandText = "INSERT INTO Authors(Name) VALUES(@Name)";


cmd.Prepare();

Here we create a prepared statement. When we write prepared statements, we use


placeholders instead of directly writing the values into the statements. Prepared statements
are faster and guard against SQL injection attacks. The @Name is a placeholder, which is
going to be filled later.

cmd.Parameters.AddWithValue("@Name", "Trygve Gulbranssen");

A value is bound to the placeholder.

cmd.ExecuteNonQuery();

The prepared statement is executed. We use the ExecuteNonQuery() method of the


MySQLCommand object when we don't expect any data to be returned. This is when we create
databases or execute INSERT, UPDATE, and DELETE statements.

$ ./prepared.exe

mysql> SELECT * FROM Authors;


+----+--------------------+
| Id | Name |
+----+--------------------+
| 1 | Jack London |
| 2 | Honore de Balzac |
| 3 | Lion Feuchtwanger |
| 4 | Emile Zola |
| 5 | Truman Capote |
| 6 | Trygve Gulbranssen |
+----+--------------------+
6 rows in set (0.00 sec)

We have a new author inserted into the table.

Retrieving data with MySqlDataReader


The MySqlDataReader is an object used to retrieve data from the database. It provides fast,
forward-only, read-only access to query results. It is the most efficient way to retrieve data
from tables.

using System;
using MySql.Data.MySqlClient;

public class Example


{

static void Main()


{
string cs = @"server=localhost;userid=user12;
password=34klq*;database=mydb";

MySqlConnection conn = null;


MySqlDataReader rdr = null;

try
{
conn = new MySqlConnection(cs);
conn.Open();

string stm = "SELECT * FROM Authors";


MySqlCommand cmd = new MySqlCommand(stm, conn);
rdr = cmd.ExecuteReader();

while (rdr.Read())
{
Console.WriteLine(rdr.GetInt32(0) + ": "
+ rdr.GetString(1));
}

} catch (MySqlException ex)


{
Console.WriteLine("Error: {0}", ex.ToString());

} finally
{
if (rdr != null)
{
rdr.Close();
}

if (conn != null)
{
conn.Close();
}

}
}
}

We get all authors from the Authors table and print them to the console.
reader = cmd.ExecuteReader();

To create a MySQLDataReader, we must call the ExecuteReader() method of the


MySqlCommand object.

while (reader.Read())
{
Console.WriteLine(reader.GetInt32(0) + ": "
+ reader.GetString(1));
}

The Read() method advances the data reader to the next record. It returns true if there are
more rows; otherwise false. We can retrieve the value using the array index notation, or use
a specific method to access column values in their native data types. The latter is more
efficient.

if (rdr != null)
{
rdr.Close();
}

Always call the Close() method of the reader when done reading.

$ ./retrieve.exe
1: Jack London
2: Honore de Balzac
3: Lion Feuchtwanger
4: Emile Zola
5: Truman Capote
6: Trygve Gulbranssen

This is the output of the example.

Column headers
Next we will show, how to print column headers with the data from the database table.

using System;
using MySql.Data.MySqlClient;

public class Example


{

static void Main()


{
string cs = @"server=localhost;userid=user12;
password=34klq*;database=mydb";

MySqlConnection conn = null;


MySqlDataReader rdr = null;

try
{
conn = new MySqlConnection(cs);
conn.Open();

string stm = @"SELECT Name, Title From Authors,


Books WHERE Authors.Id=Books.AuthorId";

MySqlCommand cmd = new MySqlCommand(stm, conn);


rdr = cmd.ExecuteReader();

Console.WriteLine("{0} {1}", rdr.GetName(0),


rdr.GetName(1).PadLeft(18));

while (rdr.Read())
{
Console.WriteLine(rdr.GetString(0).PadRight(18) +
rdr.GetString(1));
}

} catch (MySqlException ex)


{
Console.WriteLine("Error: {0}", ex.ToString());

} finally
{
if (rdr != null)
{
rdr.Close();
}

if (conn != null)
{
conn.Close();
}

}
}
}

In this program, we select authors from the Authors table and their books from the Books
table.

string stm = @"SELECT Name, Title From Authors,


Books WHERE Authors.Id=Books.AuthorId";

This is the SQL statement which joins authors with their books.

reader = cmd.ExecuteReader();

We create a MySqlDataReader object.

Console.WriteLine("{0} {1}", reader.GetName(0),


reader.GetName(1).PadLeft(18));
We get the names of the columns with the GetName() method of the reader. The
PadLeft() method returns a new string of a specified length in which the beginning of the
current string is padded with spaces. We use this method to align strings properly.

while (reader.Read())
{
Console.WriteLine(reader.GetString(0).PadRight(18) +
reader.GetString(1));
}

We print the data that was returned by the SQL statement to the terminal.

$ ./headers.exe
Name Title
Jack London Call of the Wild
Jack London Martin Eden
Honore de Balzac Old Goriot
Honore de Balzac Cousin Bette
Lion Feuchtwanger Jew Suess
Emile Zola Nana
Emile Zola The Belly of Paris
Truman Capote In Cold blood
Truman Capote Breakfast at Tiffany

Ouput of the program.

DataSet & MySqlDataAdapter


A DataSet is a copy of the data and the relations among the data from the database tables.
It is created in memory and used when extensive processing on data is needed or when we
bind data tables to a Winforms control. When the processing is done, the changes are
written to the data source. A MySqlDataAdapter is an intermediary between the DataSet
and the data source. It populates a DataSet and resolves updates with the data source.

using System;
using System.Data;
using MySql.Data.MySqlClient;

public class Example


{

static void Main()


{
string cs = @"server=localhost;userid=user12;
password=34klq*;database=mydb";

MySqlConnection conn = null;

try
{
conn = new MySqlConnection(cs);
conn.Open();
string stm = "SELECT * FROM Authors";
MySqlDataAdapter da = new MySqlDataAdapter(stm, conn);

DataSet ds = new DataSet();

da.Fill(ds, "Authors");
DataTable dt = ds.Tables["Authors"];

dt.WriteXml("authors.xml");

foreach (DataRow row in dt.Rows)


{
foreach (DataColumn col in dt.Columns)
{
Console.WriteLine(row[col]);
}

Console.WriteLine("".PadLeft(20, '='));
}

} catch (MySqlException ex)


{
Console.WriteLine("Error: {0}", ex.ToString());

} finally
{
if (conn != null)
{
conn.Close();
}

}
}
}

We print the authors from the Authors table. We also save them in an XML file. This time,
we use the MySqlDataAdapter and DataSet objects.

MySqlDataAdapter da = new MySqlDataAdapter(stm, conn);

A MySqlDataAdapter object is created. It takes an SQL statement and a connection as


parameters.

DataSet ds = new DataSet();

da.Fill(ds, "Authors");

We create and fill the DataSet.

DataTable dt = ds.Tables["Authors"];

We get the table called "Authors". We have given a DataSet only one table, but it can
contain multiple tables.
dt.WriteXml("authors.xml");

We write the data to an XML file.

foreach (DataRow row in dt.Rows)


{
foreach (DataColumn col in dt.Columns)
{
Console.WriteLine(row[col]);
}

Console.WriteLine("".PadLeft(20, '='));
}

We display the contents of the Authors table to the terminal. To traverse the data, we utilize
the rows and columns of the DataTable object.

In the next example, we are going to bind a table to a Winforms DataGrid control.

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Data;
using MySql.Data.MySqlClient;

class MForm : Form


{

private DataGrid dg = null;


private MySqlConnection conn = null;
private MySqlDataAdapter da = null;
private DataSet ds = null;

public MForm()
{

this.Text = "DataGrid";
this.Size = new Size(350, 300);

this.InitUI();
this.InitData();

this.CenterToScreen();
}

void InitUI()
{
dg = new DataGrid();

dg.CaptionBackColor = System.Drawing.Color.White;
dg.CaptionForeColor = System.Drawing.Color.Black;
dg.CaptionText = "Authors";

dg.Location = new Point(8, 0);


dg.Size = new Size(350, 300);
dg.TabIndex = 0;
dg.Parent = this;
}

void InitData()
{
string cs = @"server=localhost;userid=user12;
password=34klq*;database=mydb";

string stm = "SELECT * FROM Authors";

try
{
conn = new MySqlConnection(cs);
conn.Open();
ds = new DataSet();
da = new MySqlDataAdapter(stm, conn);
da.Fill(ds, "Authors");

dg.DataSource = ds.Tables["Authors"];

} catch (MySqlException ex)


{
Console.WriteLine("Error: " + ex.ToString());

} finally
{
if (conn != null)
{
conn.Close();
}
}
}
}

class MApplication
{
public static void Main()
{
Application.Run(new MForm());
}
}

In this example, we bind a Authors table to a Winforms DataGrid control.

using System.Windows.Forms;
using System.Drawing;

These two namespaces are for the GUI.

this.InitUI();
this.InitData();
Inside the InitUI() method, we build the user interface. In the InitData() method, we
connect to the database, retrieve the data into the DataSet and bind it to the DataGrid
control.

dg = new DataGrid();

The DataGrid control is created.

string stm = "SELECT * FROM Authors";

We will display the data from the Authors table in the DataGrid control.

dg.DataSource = ds.Tables["Authors"];

We bind the DataSource property of the DataGrid control to the chosen table.

$ dmcs -r:/usr/lib/cli/MySql.Data-6.1/MySql.Data.dll -
r:System.Windows.Forms.dll
-r:System.Drawing.dll -r:System.Data.dll dataadapter2.cs

To compile the example, we must include additional DLLs: the DLL for MySQL
connector, for the Winforms, Drawing, and for the Data.

Figure: DataGrid

Transaction support
A transaction is an atomic unit of database operations against the data in one or more
databases. The effects of all the SQL statements in a transaction can be either all committed
to the database or all rolled back.
The MySQL database has different types of storage engines. The most common are the
MyISAM and the InnoDB engines. There is a trade-off between data security and database
speed. The MyISAM tables are faster to process and they do not support transactions. On
the other hand, the InnoDB tables are more safe against the data loss. They support
transactions. They are slower to process.

using System;
using MySql.Data.MySqlClient;

public class Example


{

static void Main()


{
string cs = @"server=localhost;userid=user12;
password=34klq*;database=mydb";

MySqlConnection conn = null;


MySqlTransaction tr = null;

try
{
conn = new MySqlConnection(cs);
conn.Open();
tr = conn.BeginTransaction();

MySqlCommand cmd = new MySqlCommand();


cmd.Connection = conn;
cmd.Transaction = tr;

cmd.CommandText = "UPDATE Authors SET Name='Leo Tolstoy'


WHERE Id=1";
cmd.ExecuteNonQuery();
cmd.CommandText = "UPDATE Books SET Title='War and Peace'
WHERE Id=1";
cmd.ExecuteNonQuery();
cmd.CommandText = "UPDATE Books SET Titl='Anna Karenina'
WHERE Id=2";
cmd.ExecuteNonQuery();

tr.Commit();

} catch (MySqlException ex)


{
try
{
tr.Rollback();

} catch (MySqlException ex1)


{
Console.WriteLine("Error: {0}", ex1.ToString());
}

Console.WriteLine("Error: {0}", ex.ToString());

} finally
{
if (conn != null)
{
conn.Close();
}
}
}
}

In this program, we want to change the name of the author on the first row of the Authors
table. We must also change the books associated with this author. A good example where a
transaction is necessary. If we change the author and do not change the author's books, the
data is corrupted.

MySqlTransaction tr = null;

The MySqlTransaction is an object for working with transactions.

tr = conn.BeginTransaction();

We begin a transaction.

cmd.CommandText = "UPDATE Books SET Titl='Anna Karenina' WHERE Id=2";


cmd.ExecuteNonQuery();

The third SQL statement has an error. There is no 'Titl' column in the table.

tr.Commit();

If there is no exception, the transaction is committed.

try
{
tr.Rollback();

} catch (MySqlException ex1)


{
Console.WriteLine("Error: {0}", ex1.ToString());
}

In case of an exception, the transaction is rolled back. No changes are committed to the
database. During the rollback there could be an Exception. We handle this in a separate
try/catch statement.

$ ./transaction.exe
Error: MySql.Data.MySqlClient.MySqlException: Unknown column 'Titl' in
'field list'
at MySql.Data.MySqlClient.MySqlStream.ReadPacket () [0x00000]
at MySql.Data.MySqlClient.NativeDriver.ReadResult () [0x00000]

mysql> SELECT Name, Title From Authors, Books WHERE


Authors.Id=Books.AuthorId;
+-------------------+----------------------+
| Name | Title |
+-------------------+----------------------+
| Jack London | Call of the Wild |
| Jack London | Martin Eden |
| Honore de Balzac | Old Goriot |
| Honore de Balzac | Cousin Bette |
| Lion Feuchtwanger | Jew Suess |
| Emile Zola | Nana |
| Emile Zola | The Belly of Paris |
| Truman Capote | In Cold blood |
| Truman Capote | Breakfast at Tiffany |
+-------------------+----------------------+
9 rows in set (0.00 sec)

An exception was thrown. The transaction was rolled back and no changes took place.

However, without a transaction, the data is not safe.

using System;
using MySql.Data.MySqlClient;

public class Example


{

static void Main()


{
string cs = @"server=localhost;userid=user12;
password=34klq*;database=mydb";

MySqlConnection conn = null;

try
{
conn = new MySqlConnection(cs);
conn.Open();

MySqlCommand cmd = new MySqlCommand();


cmd.Connection = conn;

cmd.CommandText = "UPDATE Authors SET Name='Leo Tolstoy'


WHERE Id=1";
cmd.ExecuteNonQuery();
cmd.CommandText = "UPDATE Books SET Title='War and Peace'
WHERE Id=1";
cmd.ExecuteNonQuery();
cmd.CommandText = "UPDATE Books SET Titl='Anna Karenina'
WHERE Id=2";
cmd.ExecuteNonQuery();

} catch (MySqlException ex)


{
Console.WriteLine("Error: {0}", ex.ToString());

} finally {
if (conn != null)
{
conn.Close();
}
}
}
}

We have the same example. This time, without the transaction support.

$ ./notransaction.exe
Error: MySql.Data.MySqlClient.MySqlException: Unknown column 'Titl' in
'field list'
at MySql.Data.MySqlClient.MySqlStream.ReadPacket () [0x00000]
at MySql.Data.MySqlClient.NativeDriver.ReadResult () [0x00000]

mysql> SELECT Name, Title From Authors, Books WHERE


Authors.Id=Books.AuthorId;
+-------------------+----------------------+
| Name | Title |
+-------------------+----------------------+
| Leo Tolstoy | War and Peace |
| Leo Tolstoy | Martin Eden |
| Honore de Balzac | Old Goriot |
| Honore de Balzac | Cousin Bette |
| Lion Feuchtwanger | Jew Suess |
| Emile Zola | Nana |
| Emile Zola | The Belly of Paris |
| Truman Capote | In Cold blood |
| Truman Capote | Breakfast at Tiffany |
+-------------------+----------------------+
9 rows in set (0.00 sec)

An exception is thrown again. Leo Tolstoy did not write Martin Eden. The data is
corrupted.

Tweet

This was the MySQL C# tutorial, with MySQL Connector. You might be also interested in
MySQL C API tutorial, MySQL Python tutorial or MySQL PHP tutorial.

http://zetcode.com/lang/csharp/

http://zetcode.com/db/mysqlvb/

http://www.ajpdsoft.com/modules.php?name=News&file=article&sid=629
CSharp: Desarrollar aplicación C# con acceso nativo a MySQL Server mediante
ADO.NET

Explicamos en este tutorial cómo desarrollar una aplicación con el lenguaje de programación
Microsoft Visual C# .Net (de la suite de desarrollo Microsoft Visual Studio .Net 2010).

Explicamos cómo realizar una conexión nativa (sin utilizar intermediarios como ODBC ni
OLE DB) a un servidor de bases de datos MySQL Server (sea en Linux o en Windows)
desde nuestra aplicación Microsoft Visual C# .Net mediante ADO.NET (MySQL
Connector Net).

 Requisitos para desarrollar aplicación con acceso a MySQL nativo usando Visual C# .Net y
driver Connector/Net ADO.NET.
 Desarrollar aplicación C# para acceso a MySQL Server de forma nativa con ADO.NET Driver
for MySQL (Connector/NET).
 AjpdSoft Acceso MySQL con ADO.NET en C# en funcionamiento.
 Artículos relacionados.
 Créditos.

Requisitos para desarrollar aplicación con


acceso a MySQL nativo usando Visual C#
.Net y driver Connector/Net ADO.NET
A continuación indicamos los requisitos necesarios para desarrollar una aplicación, usando
el IDE de desarrollo Microsoft Visual C# .Net, que acceda de forma nativa (directa sin
intermediarios) al motor de base de datos gratuito MySQL Server:

 Suite de desarrollo Microsoft Visual Studio .Net 2010: en el siguiente tutorial explicamos
cómo instalar este entorno de desarrollo de aplicaciones .Net:

Instalar Microsoft Visual Studio .Net 2010 y desarrollar aplicación con acceso a
PostgreSQL.

 Driver Connector/Net de MySQL: necesitaremos las librerías proporcionadas por MySQL


para acceso nativo desde .Net, disponibles en el sitio web oficial de MySQL, en la URL:

http://dev.mysql.com/downloads/connector/net
En nuestro caso descargaremos la versión 6.6.4 y la plataforma (Select Platform)
"Microsoft Windows", descargaremos "Windows (x86, 32-bit), MSI Installer"
(mysql-connector-net-6.6.4.msi):

Ejecutaremos el fichero mysql-connector-net-6.6.4.msi descargado:

Pulsaremos en "Ejecutar" en la ventana de Advertencia de seguridad de Abrir


archivo:
Se iniciará el asistente de instalación de MySQL Connector Net 6.6.4, pulsaremos "Next":

Pulsaremos en "Custom" para realizar una instalación personalizada:


Los componentes a instalar:

 Entity Framework Support.


 Core Components.
 Documentation.
 Web Providers.
 Compact Framework Support.
 Visua Studio Integration.
 Samples.

Pulsaremos "Nex" para continuar con la instalación de MySQL Connector Net:


Pulsaremos "Install" para iniciar la instalación definitiva de MySQL Connector Net:
Tras la instalación de MySQL Connector Net 6.6.4, el asistente nos indicará que la
instalación ha finalizado. Pulsaremos "Finish":

 Servidor con MySQL Server: necesitaremos, obviamente, un equipo con el motor de base
de datos MySQL instalado y funcionando. En los siguientes enlaces mostramos algunos
ejemplos sobre cómo montar un servidor de MySQL Server en varios sistemas operativos:

o Cómo instalar MySQL Server en Windows XP.


o Cómo instalar MySQL Server 6.0 Alpha en Windows XP.
o Instalar y configurar MySQL Server 5 en Linux Suse 10.
o Cómo instalar MySQL Server en Linux y permitir conexiones remotas.
o Instalar y configurar MySQL Server 5 en Linux Suse 10.

Desarrollar aplicación C# para acceso a


MySQL Server de forma nativa con
ADO.NET Driver for MySQL
(Connector/NET)
Abriremos Microsoft Visual Studio .Net 2010, pulsaremos en el menú "Archivo" - "Nuevo
proyecto", seleccionaremos "Visual C#", indicaremos un nombre para la solución, por
ejemplo "AjpdSoftAccesoMySQLCSharp":

Añadiremos una referencia a la librería de MySQL ADO.NET (Connector/NET), para ello


pulsaremos en el menú "Proyecto" - "Agregar referencia":

Buscaremos el fichero "MySql.Data.dll" en la carepta de instalación de ADO.NET Driver


for MySQL (Connector/NET):
Añadiremos todos los componentes al formulario de la aplicación C#, usaremos varios
TextBox para introducir el servidor, usuario, puerto, contraseña y SQL a ejecutar.
Añadiremos ComboBox para mostrar los esquemas (bases de datos) y las tablas y varios
Button. Añadiremos también un DataGridView para mostrar el resultado de las consultas
SQL:

La descarga gratuita del código fuente completo: AjpdSoft Acceso MySQL con ADO.NET
en C#.
Añadiremos en el código la cláusula:

using MySql.Data.MySqlClient;

y añadiremos la declaración de la conexión dentro de la clase del formulario:

private MySqlConnection conexionBD;

El código C# completo de la aplicación (para los botones de conectar con servidor, usar
esquema, añadir select SQL y ejecutar consulta SQL, así como las funciones para obtener
las bases de datos de MySQL y las tablas de la base de datos seleccionada se muestra a
continuación:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace AjpdSoftAccesoMySQLCsharp
{
public partial class formAccesoMySQL : Form
{
private MySqlConnection conexionBD;

private void obtenerBasesDatosMySQL ()


{
MySqlDataReader registrosObtenidosMySQL = null;
MySqlCommand cmd =
new MySqlCommand("SHOW DATABASES", conexionBD);
try
{
registrosObtenidosMySQL = cmd.ExecuteReader();
lsBD.Items.Clear();
while (registrosObtenidosMySQL.Read())
{
lsBD.Items.Add(registrosObtenidosMySQL.GetString(0));
}
}
catch (MySqlException ex)
{
MessageBox.Show("Error al obtener bases de datos de
MySQL: " +
ex.Message,"Error al obtener catálogos",
MessageBoxButtons.OK,MessageBoxIcon.Error);
}
finally
{
if (registrosObtenidosMySQL != null)
registrosObtenidosMySQL.Close();
}
}

private void obtenerTablasBDMysql(string bd)


{
MySqlDataReader reader = null;

try
{
conexionBD.ChangeDatabase(bd);

MySqlCommand cmd = new MySqlCommand("SHOW TABLES",


conexionBD);
reader = cmd.ExecuteReader();
lsTablas.Items.Clear();
while (reader.Read())
{
lsTablas.Items.Add(reader.GetString(0));
}
}
catch (MySqlException ex)
{
MessageBox.Show("Error al obtener la lista de tablas " +
"de la BD de MySQL: " +
ex.Message,"Error obtener tablas",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
if (reader != null) reader.Close();
}
}

public formAccesoMySQL()
{
InitializeComponent();
}

private void btConectar_Click(object sender, EventArgs e)


{
if (conexionBD != null)
conexionBD.Close();

string connStr =
String.Format("server={0};port={1};user id={2};
password={3}; " +
"database=mysql; pooling=false;" +
"Allow Zero Datetime=False;Convert Zero Datetime=True",
txtServidor.Text,txtPuerto.Text, txtUsuario.Text,
txtContrasena.Text);
try
{
conexionBD = new MySqlConnection(connStr);
conexionBD.Open();

obtenerBasesDatosMySQL();
}
catch (MySqlException ex)
{
MessageBox.Show("Error al conectar al servidor de MySQL:
" +
ex.Message, "Error al conectar",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

private void btSelectSQL_Click(object sender, EventArgs e)


{
txtSQL.Text = "select * from " + lsTablas.Text;
}

private void btUsarEsquema_Click(object sender, EventArgs e)


{
obtenerTablasBDMysql(lsBD.Text);
}

private void btEjecutar_Click(object sender, EventArgs e)


{
if (opDatos.Checked)
{
DataTable tabla;
MySqlDataAdapter datosAdapter;
MySqlCommandBuilder comandoSQL;

try
{
tabla = new DataTable();

datosAdapter = new MySqlDataAdapter(txtSQL.Text,


conexionBD);
comandoSQL = new MySqlCommandBuilder(datosAdapter);
datosAdapter.Fill(tabla);

dbGrid.DataSource = tabla;
}
catch (Exception ex)
{
MessageBox.Show("Error al mostrar los datos de la
tabla [" +
lsTablas.Text + "] de MySQL: " +
ex.Message, "Error ejecutar SQL",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
if (opNoDatos.Checked)
{
try
{
int numeroRegistrosAfectados = 0;

MySqlCommand cmd = new MySqlCommand();


cmd.Connection = conexionBD;
cmd.CommandText = txtSQL.Text;
cmd.Prepare();
numeroRegistrosAfectados = cmd.ExecuteNonQuery();
MessageBox.Show("Consulta de modificación de datos "
+
"ejecutada, número de registros afectados: " +
Convert.ToString(numeroRegistrosAfectados) + ".",
"Consulta SQL ejecutada",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show("Error ejecutar consulta de " +
"modificación de datos: " +
ex.Message, "Error ejecutar SQL",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}

private void linkAjpdSoft_LinkClicked(object sender,


LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start("http://www.ajpdsoft.com");
}
}
}

AjpdSoft Acceso MySQL con ADO.NET en


C# en funcionamiento
A continuación explicamos cómo funciona AjpdSoft Acceso MySQL con ADO.NET en
C#. En primer lugar introduciremos los datos del servidor de MySQL Server al que nos
conectaremos:

 Servidor: ip o nombre DNS (hostname) del servidor con MySQL Server.


 Puerto: puerto por el que nos conectaremos, por defecto para MySQL 3306.
 Usuario: usuario de MySQL Server con permisos suficientes.
 Contraseña: contraseña del usuario anterior.

Tras introducir los datos de conexión pulsaremos en el botón "1 Conectar con servidor", si
los datos son correctos y el servidor está disponible, la aplicación obtendrá los catálogos
(bases de datos) del servidor de MySQL Server (a los que el usuario tenga permisos):

Seleccionaremos el catálogo (esquema o base de datos) que usaremos en "Usar el esquema


(BD)":
Pulsaremos en "2 Usar el esquema", si todo es correcto la aplicación obtendrá todas las
tablas del esquema seleccionado:

Seleccionaremos la tabla que usaremos para la consulta SQL y pulsaremos "3 Select (SQL)
de la tabla":

Introduciremos la consulta SQL que queramos (o dejaremos la de defecto "select * from


nombre_tabla), marcaremos "Devuelve datos" y pulsaremos "Ejecutar":

Si la consulta SQL es correcta la aplicación mostrará el resultado en el grid de datos:


Para ejecutar una consulta SQL de actualización de datos (update, delete, insert, drop,
create) marcaremos el "No devuelve datos":

Para este tipo de consultas SQL la aplicación devolverá el número de registros afectados:

Artículos relacionados
 AjpdSoft Acceso MySQL con ADO.NET en C#.
 Capturar pantalla screenshot con Visual C#.
 Separar páginas pdf en un pdf por cada página con PDFsharp y Visual C# C Sharp.
 Extraer texto y metadatos de fichero PDF con Visual C# .Net y iTextSharp.
 Instalar Microsoft Visual Studio .Net 2010 y desarrollar aplicación con acceso a
PostgreSQL.
 Convertir texto a PDF con iTextSharp y Visual Basic .Net VB.Net.
 Generar y leer códigos QR Quick Response Barcode con Visual Basic .Net VB.Net.
 Cómo desarrollar una aplicación de servicio en Windows con Visual Basic .Net.
 Estructura del código de barras EAN 13 y ejemplo en Delphi de obtención de datos.
 AjpdSoft Generador de códigos de barras.
 AjpdSoft extraer texto PDF Delphi.
 AjpdSoft Convertir Texto a PDF VB.Net.
 AjpdSoft Indexar Texto PDF C# iTextSharp.
 AjpdSoft Separar Páginas PDF código fuente Visual C# .Net.
 AjpdSoft Socket VB.Net.
 AjpdSoft Envío SMS VB.Net.
 AjpdSoft Inventario PCs - Código fuente Delphi.
 AjpdSoft Insertar Evento Google Calendar VB.net.
 AjpdSoft Envío EMail SSL VB.Net.
 AjpdSoft Conexión BD Visual Basic .Net.
 Desarrollar aplicación lector de códigos de barras para Android con App Inventor.
 Metadatos, cómo eliminarlos, cómo consultarlos, peligrosos para la privacidad.
 Insertar evento de Google Calendar con Visual Basic .Net y Google Data API.
 Crear proceso en segundo plano con barra de progreso en Visual Basic .Net VB.Net.
 Instalar Visual Studio 2010 y desarrollar aplicación con acceso a PostgreSQL.
 El control de errores en Visual Basic .Net.
 Acceso a MySQL mediante Visual Basic .Net y ODBC.
 Acceso a Oracle mediante Microsoft Visual Basic, RDO y ODBC.
 Insertar y extraer documentos en una tabla Oracle con Visual Basic 6.
 Cambiar marcadores de Word por valores del formulario de una aplicación.
 Exportar ListView a fichero CSV VB.Net.
 Función para reemplazar una cadena de texto dentro de otra - Visual Basic.
 Funciones para leer y escribir en ficheros INI VB.Net.
 Artículos, manuales y trucos del Proyecto AjpdSoft sobre Microsoft Visual Studio.
 Todos los programas con código fuente en Visual Basic.
 Foro del Proyecto AjpdSoft sobre Visual Basic, Visual C# C Sharp, VB.Net.
 Cómo instalar MySQL Server en Windows XP.
 Cómo instalar MySQL Server 6.0 Alpha en Windows XP.
 Instalar y configurar MySQL Server 5 en Linux Suse 10.
 Cómo instalar MySQL Server en Linux y permitir conexiones remotas.
 Instalar y configurar MySQL Server 5 en Linux Suse 10.
 Manual SQL con ejemplos de sentencias SQL Oracle.
 Exportar una tabla Microsoft Access a MySQL.
 Cómo cargar un fichero de texto plano en una tabla MySQL.
 Definición IDE.
 Definición URL.
 Definición SQL.
 Definición OLE DB.
 Definición ODBC.

Créditos
Artículo realizado íntegramente por Alonsojpd miembro fundador del Proyecto AjpdSoft.

You might also like