Connecting to MySQL database in C#

MySQL database association with C#: Here, we will figure out how to interface MySQL database with C#.Net with Example?

To interface with MySQL database utilizing C#, we have some prerequisite to get ready arrangement are the accompanying:

  • Establishment of MySQL in your PC.
  • Establishment of MySQL connector as indicated by PC (32-piece, 64 pieces) or as indicated by introduced Windows Operating System.
  • Establishment of Visual Studio.

Presently, we are accepting you made all the above establishments. To start with, we will open the MySQL terminal window, that seems this way,

Connecting to MySQL database in C#

Presently, we will execute underneath SQL direction to make a database.

mysql> make database mysqltest;

Utilizing “show databases” order we can perceive what number of databases is accessible in MySQL.

Presently, we need to make an application in Visual Studio, that shows the network with MySQL. Here, we will build up a windows application. (Note: Before making an application first we have to include the reference for MySQL connector in arrangement adventurer, see the picture beneath)

Connecting to MySQL database in C#

In arrangement pilgrim window, we have included a reference “MySql.Data”, on the grounds that doesn’t drop as a matter of course, we need it physically, on the off chance that MySql connector is introduced in your PC, at that point right-click on “Reference” and afterwards, you can include it, without including MySql.data reference we can’t associate with MySql database utilizing C# program.

C# venture with MySQL database network

Presently, we look to the application. Here, we took windows structure with one order button.

Connecting to MySQL database in C#

In the above example, we changed two properties of both window structure and order button are the accompanying:

  • Name
  • Text

Structures properties

    Name:  "frmMySqlConn"
    Text:	"MY-SQL Connection test"

Button properties

    Name:	"btnConnect"
    Text:	"Connect"

C# source code to interface with MySQL database

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MySql.Data.MySqlClient;


namespace MySqlConnect
{
    public partial class frmMySqlConn : Form
    {
        public frmMySqlConn()
        {
            InitializeComponent();
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            string MyConStr = "Server=localhost;Database=mysqltest;uid=root;pwd=root";

            MySqlConnection conn = new MySqlConnection(MyConStr);

            conn.Open();

            if (conn.State == ConnectionState.Open)
            {
                MessageBox.Show("Connection Opened Successfully");
                conn.Close();
            }
        }
    }
}

In the above code, the greater part of the code is auto-created we need to make the accompanying changes,

  • Included namespace
  • Composed code in button click occasion

Here, we added one extra namespace to utilize classes with respect to MySQL network.

utilizing MySql.Data.MySqlClient; 

On button click occasion, we made an association string that contains SERVER, SERVER could be “localhost” or we can likewise give the IP address of the server, for our situation MySQL is introduced on our PC. At that point, we are utilizing the “localhost” as a SERVER and the database name may be “mysqltest” alongside the username and secret phrase of MySQL database.

Here, we have MySqlConnection class, at that point, we pass the association string while object creation, it is likewise conceivable to give association string after article creation. At that point we are utilizing the “Open() strategy” for the database association, on the off chance that we pass the right association string with right qualifications, it will be associated with the database effectively, else, it will produce an exemption at the run time this way.

Mistake Message on association come up short:

Connecting to MySQL database in C#

Association Successful message:

Connecting to MySQL database in C#

To check the effective association, we can check the association state, on the off chance that it is associated effectively, we can show the mistake message and can close the association.

Leave a Comment

error: Alert: Content is protected!!