o7planning

Connect to MySQL database in C#

  1. Download MySQL Connector for Dotnet
  2. Connect to MySQL using C#
  3. Working with MySQL Database using C#
  4. Appendix: Connection errors and how to fix it

1. Download MySQL Connector for Dotnet

You need to login to download. You can register freely an account. This is the result downloaded:

2. Connect to MySQL using C#

Create a Project named ConnectMySQL:
Project is created, you need to declare Reference with MySql.Data.dll.
Create some utility class to help connect to the MySQL database:
DBMySQLUtils.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;

namespace Tutorial.SqlConn
{
    class DBMySQLUtils
    {

        public static MySqlConnection
                 GetDBConnection(string host, int port, string database, string username, string password)
        {
            // Connection String.
            String connString = "Server=" + host + ";Database=" + database
                + ";port=" + port + ";User Id=" + username + ";password=" + password;

            MySqlConnection conn = new MySqlConnection(connString);

            return conn;
        }
       
    }
}
DBUtils.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;

namespace Tutorial.SqlConn
{
    class DBUtils
    {
        public static MySqlConnection GetDBConnection( )
        {
            string host = "192.168.205.130";
            int port = 3306;
            string database = "simplehr";
            string username = "root";
            string password = "1234";

            return DBMySQLUtils.GetDBConnection(host, port, database, username, password);
        }
       
    }
}
Test Connection:
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tutorial.SqlConn;
using MySql.Data.MySqlClient;

namespace ConnectMySQL
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Getting Connection ...");
            MySqlConnection conn = DBUtils.GetDBConnection();

            try
            {
                Console.WriteLine("Openning Connection ...");

                conn.Open();

                Console.WriteLine("Connection successful!");
            }
            catch(Exception e)
            {
                Console.WriteLine("Error: " + e.Message);
            }

            Console.Read();
        }
    }

}
Running Program class to test the connection:
Getting Connection ...
Openning Connection ...
Connection successful!

3. Working with MySQL Database using C#

Next tutorial - Working with MySQL Database using C#:
Content included:
  • Insert
  • Update
  • Delete
  • Call Function and Procedure.

4. Appendix: Connection errors and how to fix it

In case you connect to MySQL Database in other computer, you may receive an error as illustrated below, which caused by MySQL is disabling connections from another computer, you need to configure MySQL to allow this. You can see the instructions at:
Error: Host '192.168.205.134' is not allowed to connect to this MySQL server
If your MySQL is installed in another compter (with Windows OS), you need to open firewall for port 3306 (On computer running MySQL)