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
Username=Ehtesham
Password=1234
Note: You need to include this assembly.
- using MySql.Data.MySqlClient;
Insert Data
- private void button1_Click(object sender, EventArgs e)
- {
- try
- {
-
- string MyConnection2 = "datasource=localhost;port=3307;username=root;password=root";
-
- 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+ "');";
-
- MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
-
- MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2);
- MySqlDataReader MyReader2;
- MyConn2.Open();
- MyReader2 = MyCommand2.ExecuteReader();
- MessageBox.Show("Save Data");
- while (MyReader2.Read())
- {
- }
- MyConn2.Close();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
Update Data
- private void button2_Click(object sender, EventArgs e)
- {
- try
- {
-
- string MyConnection2 = "datasource=localhost;port=3307;username=root;password=root";
-
- 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 + "';";
-
- MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
- MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2);
- MySqlDataReader MyReader2;
- MyConn2.Open();
- MyReader2 = MyCommand2.ExecuteReader();
- MessageBox.Show("Data Updated");
- while (MyReader2.Read())
- {
- }
- MyConn2.Close();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
Delete Data
- private void button3_Click(object sender, EventArgs e)
- {
- try
- {
- string MyConnection2 = "datasource=localhost;port=3307;username=root;password=root";
- string Query = "delete from student.studentinfo where idStudentInfo='" + this.IdTextBox.Text + "';";
- MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
- MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2);
- MySqlDataReader MyReader2;
- MyConn2.Open();
- MyReader2 = MyCommand2.ExecuteReader();
- MessageBox.Show("Data Deleted");
- while (MyReader2.Read())
- {
- }
- MyConn2.Close();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
Display Data
- private void button4_Click(object sender, EventArgs e)
- {
- try
- {
- string MyConnection2 = "datasource=localhost;port=3307;username=root;password=root";
-
- string Query = "select * from student.studentinfo;";
- MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
- MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2);
-
-
- MySqlDataAdapter MyAdapter = new MySqlDataAdapter();
- MyAdapter.SelectCommand = MyCommand2;
- DataTable dTable = new DataTable();
- MyAdapter.Fill(dTable);
- dataGridView1.DataSource = dTable;
-
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
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.