Using MySQL with .Net

Required Components:

  • MySQL 5.0 database server
  • MySQL Connector/Net 1.0.6 or higher
  • MySQL Administrator 1.1 or higher
  • .NET Framework 1.0 or higher
  • Visual Studio 2002 or higher

Download and install MySQL Community Server
MySQL Community Edition is a freely downloadable version of the world’s most popular open source database that is supported by an active community of open source developers and enthusiasts.

Download Connector/Net
Connector/Net is a fully-managed ADO.NET driver for MySQL.

  • Download link : Connector/Net
  • Unzip the ZIP file to get the .MSI file and install it by double clicking on it.

Connect to MYSQL from .NET

  • Go to the solution explorer in Visual Studio and select Add References option.
  • From Add References dialog box, select “MySQL.Data” option and click on Ok.
  • Refer the following code to connect to MYSQL.
// Connection string for a typical local MySQL installation
string cnnString = 
 "Server=localhost;Port=3306;Database=versedb;Uid=root;Pwd=MySecretPassword";

// Create a connection object and data adapter
MySqlConnection cnx = new MySqlConnection(cnnString);
MySqlDataAdapter adapter = new MySqlDataAdapter();

// Create a SQL command object
string cmdText = "SELECT * FROM verse";
MySqlCommand cmd = new MySqlCommand(cmdText, cnx);

// Create a fill a Dataset
DataSet ds = new DataSet();
adapter.SelectCommand = cmd;
adapter.Fill(ds);

// Bind the DataSet
// ... Place your databinding code here ...

If you’re already accustomed to data binding in .NET, 
you should immediately recognize the similarities.  
The only difference is you use the MySqlXxx classes 
instead of the OleDbXxx or SqlXxx classes.
Source/ Reference Links:

Leave a Reply

Your email address will not be published. Required fields are marked *