Connect to mysql database using C#


I searched Internet a lot before I could figure out how to connect to mysql database server using C#. This is the one that worked in my favour.

To connect to mysql database using C# first you should have to download a small program called mysql connector .net from MySQL’s official site. Click here to visit this link to download mysql connector .net

After installing connector. Open Visual Studio and then create a new console application project. Now you have to add the reference of the namespace for mysql connection.

Go to project>Add Reference…

In the .NET tab you shall find a component named “MySql.Data”. Select this component and click “Add”.

Add the Name space at the top of your code.

using MySql.Data.MySqlClient;

In this string change your server, database name, user id and password

public static string db = "server=localhost;database=abc;uid=root;password="

Now finally use this code to select data from your database.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;

namespace ConsoleApplication3
{
    class Program
    {
        public static string db = "server=localhost;database=dsc;uid=root;password=";
        static void Main(string[] args)
        {
            try
            {
                MySqlConnection con = new MySqlConnection(db);
                con.Open(); // connection must be openned for command
                MySqlCommand cmd = new MySqlCommand("Select * FROM `dsc_scripts`", con);
                MySqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    Console.WriteLine(reader.GetString("id") + ": " + reader.GetString("name") + " - " + reader.GetString("hs"));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: "+ex);
            }
            finally
            {
                con.Close();
            }
        }
    }
}

Hope this will be helpful for you.

No related posts.

To make money we lose our health, and then to restore our health we lose our money.... We live as if we are never going to die, and we die as if we never lived!

6 Comments Leave yours

  1. Nice tutorial……
    Thank you……..

  2. DM #

    Thank you

  3. H.King #

    I have to connect MySQL server in my window C# program.
    MySQL is not in localhost, in real website.
    I test localhost MySQL is OK but I don’t know how to write mysql connection string to link real website.

    • just making the correct connection link would not work. you must have to make sure your mysql server allow remote acess from that ip. making the connection string is not difficult, you just have to change the localhost to your remote machines ip or domain name

  4. Herock #

    Thank’s borhan vai valo kisu resouce’s rakhar jonno

  5. I downloaded the MySql Connector .NET from the link you provided. When I try to “Add Reference…” there is nothing that says MySql.Data under the .NET tab.

    The MySql Connector was downloaded to my Downloads folder. Do I need to move it to another location? What am I doing wrong? Or what haven’t I done?

Leave a Reply