Connection with MySQL in C#.net

Many time we comes in a situation where we cannot use Sql Server with our C#.net application. There can be many problems like Budget,Resources. In such situation we think of a Database which can be a good alternative to Sql Server. at that time one names that comes in most of the developer is MySql as it is an open source database. In this tutorial we will discuss how to connect you C#.net application with MySql Database.

For making connection with MySQL in C#.net you have to do the following tasks.

  1. Download and install the MySQL dotnet connector. Click here to download.
  2. After installing the connector add the reference to the MySql.Data to your solution.

    Add Reference to MySql.Data

    Add Reference to MySql.Data

  3. Go to you class file and add the MySql.Data.MySqlClient namespace in your application
  4. Below is the code to make the connection and read the data from database.
    string constring = "Server=127.0.0.1;Uid=Demo;Pwd=demo@123;Database=demo;";
    MySqlConnection connection = new MySqlConnection(constring);
    connection.Open();
    MySqlCommand command = new MySqlCommand("Select * from TBL_Student", connection);
    MySqlDataReader rdr = command.ExecuteReader();
    while (rdr.Read())
    {
    	Studentinfo.Add (new Student(){
    		id = int.Parse(rdr["id"].ToString()),
    		name = rdr["name"].ToString()   
    	}) ; 
    }
    connection.Close();
  5. After making the connection with the MySql database you can work as you are working with Sql Server.

Keep visiting  for more post.

Rajat Jain
Rajat Jain

Latest posts by Rajat Jain (see all)

12 Comments
  1. doudoune armani ea7 homme
  2. webpage
    • admin
  3. parajumpers rea
  4. web site
  5. doudoune north face homme
    • admin
  6. Mohammad
    • admin
  7. pjs doudoune prix
  8. Parajumpers pels
  9. parajumpers

Leave a Reply