How to make a Connection between c# and mysql?

Database @Image courtesy of ddpavumba/

How to make a Connection between c# and mysql?

Welcome to CodeSpread!

This is my first article on, titled ‘Connection between c# and mysql’.Before going into details, we should know that mysql is a open source software for data base management system. We use Microsoft visual c# and sql data source quite often , But in this tutorial I will show how we can connect mysql database and c# windows application.

Screen 1 : We have created a demo application. Inside it,we place a Button.


Where on the button click , we can create a table to the specified mysql database.

Screen2 : We have created a table in mysql database.


Next, In c# class file, import these namespaces.

using System;

using System.Collections.Generic;

using System.Drawing;

using System.Windows.Forms;

using MySql.Data.MySqlClient ;

Where the above namespace “MySql.Data.MySqlClient” is the dll file ,in which all the connection commands will be residing .This can be found in mysql connector to c#/.net.Once you have imported the dll, you need to add this reference to your current project.

namespace DemoApplication


/// <summary>

/// Description of MainForm.

/// </summary>

public partial class MainForm : Form


public MainForm()


// // The InitializeComponent() call is required for Windows Forms designer support. //

InitializeComponent(); // // TODO: Add constructor code after the InitializeComponent() call. //


/* here What I have done is on button click, I have inserted a set of commands that creates a table in mysql database */

public void Button1Click(object sender, EventArgs e)


string quer= "SERVER=localhost; " + "DATABASE=sathish;" + "USERID=root;" + "PASSWORD=root;" ;

/*The above command is used to connecting the mysql with c# , Where you need to select appropriate database ,userid and password */

MySqlConnection con = new MySqlConnection(quer); con.Open() ;

/* You can use mysqlcommand or command builder for running the querries */

MySqlCommand cmd=new MySqlCommand();


/*Specifcation of the mysql connection over here */

cmd.Connection=con ;

/*Command text is where we need to insert our queries */

cmd.CommandText="create table Demo(userid varchar(20),Password varchar(20));" ;

/*Lastly, we need to execute the queries using ExecuteNonQuery of Mysqlcommand */



catch(MySqlException ex)


label1.Text = ex.Message;





Note: I prefer to build Mysqlconnection with an exception block so it would be easier diagnosing the error.
You can download mysql connector and mysql to your system from . Next I’ll come with how you can insert,update and select operations using DataGrid.I hope, I was able to help you.For any queries, please comment over here or send your queries or articles to

Author: Sathish

I just loved to express myself...

Share This Post On


  1. nice superb explaination

    Post a Reply
  2. thanks for the tutorial
    I am having a problem.
    The feature “Create SQL server database” isn’t available , I can’t select it.
    Can you help, please ?

    Post a Reply

Submit a Comment

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

More from CodeSpread:

  • C# Code to send a MailC# Code to send a MailWhile working in a project, we always neglect the importance of the code which we have written many times and now end up in copy pasting the code from old projects. Lately, I saw a request in fo...
  • 23 Dec: Must Read Codes [11-15]23 Dec: Must Read Codes [11-15]We are sharing few must read codes/concepts which are required now and then. Each of these codereads doesn’t require a full length descriptive article so combining them into groups of five. CodeRe...
  • Get Educated: C# language 5.0 SpecificationsGet Educated: C# language 5.0 SpecificationsVery often,Developers ask me to write something about C# Language Specifications and I always ask them to refer Microsoft Download center. I think this is a pretty easy act as C# Language Specifica...
  • RegisterClientScriptBlock, RegisterClientScriptIncludeRegisterClientScriptBlock, RegisterClientScriptInclude"Current trends are more inclined towards the client model for a better UI experience where small pieces of client script are integrated into the application." These client scripts can be declarat...
  • How to view Intermediate Code in C# using Ildasm.ExeHow to view Intermediate Code in C# using Ildasm.ExeHi guys, this is Sathish again, today let’s discuss how to view the intermediate code in c#. We know that CLR (Common Language Runtime) is used to convert the MSIL (Microsoft Intermediate Language...
  • Asp.Net: More about CookiesAsp.Net: More about CookiesWe have seen, how our cookies looks in our last article Cookies Part 1. In this article, we will talk about their properties,limitations and technical part. Properties and Limitations Cook...
  • Try one ORM: Entity FrameworkTry one ORM: Entity FrameworkAs promised in the last article, We are going to share our experience of Entity Framework Ver 4.1. First of all, we have to add and configure Entity Framework in visual studio. We are going to u...
  • My first experience with LISTS in C#My first experience with LISTS in C#Hi guys, this is Sathish, Lets discuss the concepts of lists. These are the concepts which I have planned to discuss with you guys today. What is a List and how it is advantageous than array...