How to connect MySQL to C#

       MySQL provides a number of classes in the MySQL Connector / Net to connect the MySQL database to a C #application. A MySqlConnection object routes all communications between a C #application and the MySQL server. Therefore, a MySqlConnection object must be instantiated , configured and opened before the application can communicate with the server.

Download MySQL Connector / Net

From the MySQL developer website you can download MySQL Connector / Net for free.

Click the link below…. Download MySQL Connector / Net

      You need to add the MySQL reference to your project before you start connecting your application to MySql. To do this, right-click on the project name, select Add Reference and select “MySql. Data” from the project name.

Next, in your C #project, you need to add MySql Library.

Use MySql. Data. MySqlClient; C #MySQL connection string myConnectionString=’ server= localhost; database= SDB1;uid= Pad;pwd= xyz321;’

C# program

MySqlConnection object, assigning the connection string and opening the connection.

using System;

using System.Windows.Forms;

using MySql.Data.MySqlClient;

namespace WindowsApplication


   public partial class FormA : Form


       public FormA()




       private void bb1_Click(object sender, EventArgs ex)


           string connetionString = null;

           MySqlConnection cnn ;

                       connetionString = “server=localhost;database=SDB1;uid=Pad;pwd=xyz321;”;

           cnn = new MySqlConnection(connetionString);




              MessageBox.Show (“Connection Open !!! “);



           catch (Exception ex)


               MessageBox.Show(“Can not open connection !!! “);





More servers:

myConnectionString = Server=server001, server002;database=SDB1;uid=Pad;pwd=xyz321;”;

Specifying the TCP port


The default port for MySql is 3306 and the value will be ignored if you use a Unix socket.

Note: MySQL Connector / Net is not compatible with Microsoft products ‘ Express versions, including Visual Web Developer from Microsoft.

About the author


Hi, I'm Padmarao Avisetty, an aspiring blogger with an obsession with everything related to technology. This blog is dedicated to helping people learn about technology and technology-related solutions.

View all posts


Leave a Reply

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