Connect MySql Database with C#

First go to MySQL site and download MySQL Connector.
After you installed it, you are ready to go. Its that simple and easy.
Here is an example

Add Mysql.Data assembly reference

  1. using System.Data;
  2. using MySql.Data.MySqlClient;
  3.  
  4. public DataTable GetDataFromMySql()
  5. {          
  6.     DataSet ds = new DataSet();
  7.     MySqlDataAdapter da;
  8.     MySqlConnection con = new MySqlConnection("Server=your_server;Database=your_db;Uid=your_username;Password=your_password");
  9.  
  10.     try
  11.     {
  12.         MySqlCommand cmd = new MySqlCommand("select * from your_table", con);
  13.         con.Open();
  14.         da = new MySqlDataAdapter(cmd);
  15.         da.Fill(ds);                
  16.     }
  17.     catch
  18.     {
  19.         throw;
  20.     }
  21.     finally
  22.     {
  23.         if (con.State == ConnectionState.Open)
  24.             con.Close();
  25.     }
  26.  
  27.     return ds.Tables[0];
  28. }


Happy Coding
m ant / Mechanical Ant