C#  

Insert, Update, Delete, Display Data in MySQL Using C#

This article shows how to insert, update, delete and display data in MySQL.

I have updated the Article and source code on my personal blog. you can get it here.

Introduction

MySQL is a fast, easy-to-use RDBMS being used for many small and big businesses. We can use MySQL with C#, Java and many other languages. Here we will use C#.

Diagram 1

Diagram

Username=Ehtesham
Password=1234


Note: You need to include this assembly.

  1. using MySql.Data.MySqlClient;  //Its for MySQL  

 

Insert Data

  1. private void button1_Click(object sender, EventArgs e)  
  2. {  
  3.     try  
  4.    {  
  5.             //This is my connection string i have assigned the database file address path  
  6.             string MyConnection2 = "datasource=localhost;port=3307;username=root;password=root";  
  7.             //This is my insert query in which i am taking input from the user through windows forms  
  8.             string Query = "insert into student.studentinfo(idStudentInfo,Name,Father_Name,Age,Semester) values('" +this.IdTextBox.Text+ "','" +this.NameTextBox.Text+ "','" +this.FnameTextBox.Text+ "','" +this.AgeTextBox.Text+ "','" +this.SemesterTextBox.Text+ "');";  
  9.             //This is  MySqlConnection here i have created the object and pass my connection string.  
  10.             MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);  
  11.             //This is command class which will handle the query and connection object.  
  12.             MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2);  
  13.             MySqlDataReader MyReader2;  
  14.             MyConn2.Open();  
  15.             MyReader2 = MyCommand2.ExecuteReader();     // Here our query will be executed and data saved into the database.  
  16.             MessageBox.Show("Save Data");  
  17.             while (MyReader2.Read())  
  18.             {                     
  19.             }  
  20.             MyConn2.Close();  
  21.         }  
  22.         catch (Exception ex)  
  23.         {   
  24.             MessageBox.Show(ex.Message);  
  25.       }  
  26. }  

Update Data

  1. private void button2_Click(object sender, EventArgs e)  
  2. {  
  3.     try  
  4.     {  
  5.         //This is my connection string i have assigned the database file address path  
  6.         string MyConnection2 = "datasource=localhost;port=3307;username=root;password=root";  
  7.         //This is my update query in which i am taking input from the user through windows forms and update the record.  
  8.         string Query = "update student.studentinfo set idStudentInfo='" + this.IdTextBox.Text + "',Name='" + this.NameTextBox.Text + "',Father_Name='" + this.FnameTextBox.Text + "',Age='" + this.AgeTextBox.Text + "',Semester='" + this.SemesterTextBox.Text + "' where idStudentInfo='" + this.IdTextBox.Text + "';";  
  9.         //This is  MySqlConnection here i have created the object and pass my connection string.  
  10.         MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);  
  11.         MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2);  
  12.         MySqlDataReader MyReader2;  
  13.         MyConn2.Open();  
  14.         MyReader2 = MyCommand2.ExecuteReader();  
  15.         MessageBox.Show("Data Updated");  
  16.         while (MyReader2.Read())  
  17.         {  
  18.         }  
  19.         MyConn2.Close();//Connection closed here  
  20.     }  
  21.     catch (Exception ex)  
  22.     {   
  23.         MessageBox.Show(ex.Message);  
  24.     }  
  25. }  

Delete Data

  1. private void button3_Click(object sender, EventArgs e)  
  2. {  
  3.     try  
  4.     {  
  5.         string MyConnection2 = "datasource=localhost;port=3307;username=root;password=root";  
  6.         string Query = "delete from student.studentinfo where idStudentInfo='" + this.IdTextBox.Text + "';";  
  7.         MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);  
  8.         MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2);  
  9.         MySqlDataReader MyReader2;  
  10.         MyConn2.Open();  
  11.          MyReader2 = MyCommand2.ExecuteReader();  
  12.          MessageBox.Show("Data Deleted");  
  13.          while (MyReader2.Read())  
  14.          {  
  15.          }  
  16.          MyConn2.Close();  
  17.     }  
  18.     catch (Exception ex)  
  19.     {  
  20.         MessageBox.Show(ex.Message);  
  21.     }  
  22. }  

Display Data

  1. private void button4_Click(object sender, EventArgs e)  
  2. {  
  3.     try  
  4.     {  
  5.         string MyConnection2 = "datasource=localhost;port=3307;username=root;password=root";  
  6.         //Display query  
  7.         string Query = "select * from student.studentinfo;";  
  8.         MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);  
  9.         MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2);               
  10.         //  MyConn2.Open();  
  11.        //For offline connection we weill use  MySqlDataAdapter class.  
  12.        MySqlDataAdapter MyAdapter = new MySqlDataAdapter();    
  13.        MyAdapter.SelectCommand = MyCommand2;  
  14.        DataTable dTable = new DataTable();  
  15.        MyAdapter.Fill(dTable);  
  16.        dataGridView1.DataSource = dTable; // here i have assign dTable object to the dataGridView1 object to display data.               
  17.         // MyConn2.Close();  
  18.    }  
  19.   catch (Exception ex)  
  20.   {   
  21.         MessageBox.Show(ex.Message);  
  22.    }  
  23. }  

Diagram 2

Diagram 2

I have also attached the source code so you can download it. Remember that you need to make your database and also your connection strings and so on.