How to Connect to MySQL Database through C#

Scenario

In my last blog (link), I have gone through how to migrate a dBase database to MySQL database. After that, we had implemented a window application in .net using c#. And as per the requirement, we had to communicate with the database.

Here I am going through, how to connect C# with MySQL Database.

Procedure

To work with MySQL database with C# application, MySQL provides numbers of different objects for different purpose. MySqlConnection Object is one of them, which is works like the road between MySQL and .net application. So, before our application communicates with the database, we must instantiate, configure, and open a MySqlConnection object. I found a very good blog, which provide quite detail information about this.

Step 1: Download and install the appropriate MySQL Connector (64bit or 32bit) from MySQL online repository.

 

Once downloaded and installed in the system. It’s time to establish the connection.

Step 2: In solution explorer create a new project.
 

In add new project window add “class library” project.
 
 
Step 3: Select the project from solution explorer. Add reference of MySql.Data in the project from “reference manager”.


Check in your reference MySql.data is added or not.

 

Step 4: Now it’s time to create connection with database.


The connection string for database connection is “server=localhost; user=root; database=[Database name]; port=3306; password=[password];”.