Monday, November 24, 2008

Connect to a MySQL 5.0 database with ADO.NET

If you want to develop applications with the .NET Framework having a MySQL database as a data source you may want to try the MySQL Connector/NET 5.2. The MySQL Connector/NET is a fully managed ADO.NET component wich provides specific implementation for all ADO.NET classes. You can download the MySQL Connector/NET 5.2 component from the MySQL web site.

MySQL Connector/NET 5.2 main features

  • A new profile provider has been implemented along with a fully revamped provider schema.
  • New bulk loading and script execution classes are available.
  • The ability to clear a single or all connection pools.
  • Integration into Visual Studio 2008
  • MySqlDataAdapter now supports batching
  • BINARY(16) columns are now treated as GUIDs
  • Various changes in how we handle parameters.

How to use the MySQL Connector/NET

In order to use this component you have to add a reference to the MySql.Data.dll (from the MySQL Connector/NET package you downloaded form MySQL web site).

C# .NET

using MySql.Data.MySqlClient;
...
string connString = "SERVER=localhost;" +
                            "DATABASE=mySQLDatabase;" +
                            "UID=user;" +
                            "PASSWORD=password;";
 
MySqlConnection conn = new MySqlConnection(connString);
MySqlCommand command = conn.CreateCommand();
MySqlDataReader reader;
 
command.CommandText = "select * from table";
conn.Open();
 
reader = command.ExecuteReader();
 
while (reader.Read())
{
    // Read the value...
    string value = reader.GetValue(0).ToString();
}
 
conn.Close();


Here you can read more about how to use command parameters with MySQL and C#: The ADO.NET command parameters

kick it on DotNetKicks.com

1 comment:

Anonymous said...

Hi,
Publish your blog content URL at www.dotneturl.com and get .NET reader from us.