Connect C# with MySql

By | July 30, 2012

This article is to show the step wise process of how to connect C# to MySql, I will be using MySql Connect/Net for this. Later I will show how to execute INSERT, UPDATE, DELETE and SELECT commands, this will include only setting up the connection.

Getting Started

First make sure you have downloaded and installed the MySql Connector/Net from the MySQL official website. In this article, I will use the Connector/NET version 6.1.6.

I felt the need of this when I was developing a windows form application and wanted to connect it to a database on my website. In this article I’m assuming that you have a cpanel on web and a database in it, you can also install MySql on your system.

Our first step will be “Adding Reference and Creating the MySQL Connector DLL from the Project”.

First we need to add a reference to ‘mysql’, we can do this by navigating to solution explorer and then right-click on the reference folder and then look for MySql.Data under .net tab

In order to use the application on other computers that don’t have the connector installed, we will have to create a DLL from the reference. To do so, we right click the reference name in our project, and set the copy local to true in its properties:

Now we are done with adding the reference and now we can start our actual coding.

We will start by adding a namespce

using MySql.Data.MySqlClient;

I have created a simple form with a button in it, our connection will be established when the button is clicked i.e. we will write our code in the OnClicked event of the button. My form looks some what like this.

Now comes the actual coding part.

We will have to create the connection string first.

To establish a connection, you must specify the computer you are connecting to, that has MySQL installed. To indicate the computer you are connecting to, use the Data Source, theDataSource, the Host, the Server, the Addr, the Address, or the Network Address attribute of the connection string. If you are connecting to a local database (installed in the same computer where the application is running), you can omit specifying the name of the computer. Otherwise, you can assign localhost to this attribute. To specify the database you want to connect to, the connection string includes an attribute named Database or Initial Catalog.

To specify the user name, use the User Id, the Uid, theUser name, or the Username attribute and for password use the PASSWORD or the PWD (remember that the attributes are not case-sensitive) attribute.

Here is a sample connection string :

string cs = @”server=YOUR_SERVER_NAME;userid=YOUR_USERNAME;
password=YOUR_PASSWORD;database=DATABASE_NAME”;

Now the code under the button clicked event.

That’s all. Debug your application, i have added the connection open in try catch block so if the connection established it will show a message boc which says “Connection Established”.

Cheers.

Leave a Reply

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