How to connect MySQL database in Visual Studio

This tutorial guide you to connect MySQL database in Visual Studio. This consist of four parts, those are how to connect visual studio with MySQL database, data insert to Mysql database, data update and data delete from MySQL database using c# windows form application. Use the below link to download this complete project.

Part 01 Connect windows form application with MySQL database.

Follow video tutorial with bellow source codes to connect MySQL database in Visual Studio

Database Connection Class

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

namespace TestProject
{
    class DBConnect
    {
        public static MySqlConnection ConnectDB()
        {
            try
            {
                string server = "localhost";
                string database = "testproject";
                string uid = "your_database_user_name";
                string password = "your_password";
                string connectionString;
                connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
                MySqlConnection connection = new MySqlConnection(connectionString);
                return connection;
            }
            catch (MySqlException ex)
            {
                return null;
            }
        }
    }
}

Remember to add MySql.Data.DLL reference file to the project. Download that reference file using this link.

 connect MySQL database in Visual Studio - reference

Part 02 Insert data using the created database connection.

Use bellow codes to insert data to the MySQL database. I have created a table with two columns. The column names are “id” and “name”.

MySqlConnection con = DBConnect.ConnectDB(); //DBConnect is database connection class name and ConnectDB is connection method.  
con.Open();
            try
            {
                string qu = "INSERT INTO `your_table_name`(`id`, `name`) VALUES ('"+txtid.Text+"', '"+txtnames.Text+"')";
                MySqlCommand cm = new MySqlCommand(qu, con);
                cm.ExecuteNonQuery();
                MessageBox.Show("Data insert");
            }
            catch (Exception ex)
            {

            }
            con.Close();

Part 03 update Mysql database data using c# application.

You can Use bellow codes to update data.


MySqlConnection con = DBConnect.ConnectDB(); //DBConnect is database connection class name and ConnectDB is connection method.                 
con.Open();
                string su = "UPDATE `your_table_name` SET `name`='"+txtnames.Text+"' WHERE id='"+txtid.Text+"'";
                MySqlCommand cm = new MySqlCommand(su, con);
                cm.ExecuteNonQuery();
                con.Close();

Part 04 Delete data from Mysql database using c# application.

You can Use bellow codes to delete data.

MySqlConnection con = DBConnect.ConnectDB(); //DBConnect is database connection class name and ConnectDB is connection method.   
con.Open();
                string su = "DELETE FROM `your_table_name` WHERE id='"+txtid.Text+"'";
                MySqlCommand cm = new MySqlCommand(su, con);
                cm.ExecuteNonQuery();
                con.Close();

Watch below video tutorial to follow this tutorial step by step.

What are the languages, technologies and platform used to develop this project?

  • C# programming language
  • Mysql database (wamp server)
  • Visual Studio.

How to use this 
You can download this project source code and database using the below link.

The goals of providing this project source code:

  • To provide an example for students to develop their own projects.
  • helping people create their projects.
  • sharing knowledge and codes for educational purpose.

This project source codes not for:

  • You cannot use this project or project source codes for commercial purposes
  • You cannot re-upload this project or project source code to web internet or any kind of space.

Copyright © codeguid

Do you need help to remotely set up my any project on your machine or customize any project with your requirement please contact  syntech1994@gmail.com

Download Source codes

42 Downloads

Check more projects>>

Be the first to comment

Leave a Reply

Your email address will not be published.


*