This article shows how to insert, update, delete, and display data in MySQL.
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; //Its for MySQL
Insert Data
private void button1_Click(object sender, EventArgs e)
{
try
{
//This is my connection string i have assigned the database file address path
string MyConnection2 = "datasource=localhost;port=3307;username=root;password=root";
//This is my insert query in which i am taking input from the user through windows forms
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+ "');";
//This is MySqlConnection here i have created the object and pass my connection string.
MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
//This is command class which will handle the query and connection object.
MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2);
MySqlDataReader MyReader2;
MyConn2.Open();
MyReader2 = MyCommand2.ExecuteReader(); // Here our query will be executed and data saved into the database.
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
{
//This is my connection string i have assigned the database file address path
string MyConnection2 = "datasource=localhost;port=3307;username=root;password=root";
//This is my update query in which i am taking input from the user through windows forms and update the record.
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 + "';";
//This is MySqlConnection here i have created the object and pass my connection string.
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();//Connection closed here
}
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";
//Display query
string Query = "select * from student.studentinfo;";
MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2);
// MyConn2.Open();
//For offline connection we weill use MySqlDataAdapter class.
MySqlDataAdapter MyAdapter = new MySqlDataAdapter();
MyAdapter.SelectCommand = MyCommand2;
DataTable dTable = new DataTable();
MyAdapter.Fill(dTable);
dataGridView1.DataSource = dTable; // here i have assign dTable object to the dataGridView1 object to display data.
// MyConn2.Close();
}
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.

Randy TomlinsonPosted Apr 8, 2023, 8:14 PM
Dude, the script have a few erros. i had to fix it first before it worked. especialy in the Form1 i wont load form2. and in Form2 the show data code is completely missing. i had to grab it from here.
Ammar ShaukatPosted Jun 27, 2022, 7:56 AM
Glad to see your article here... please write another article to write into the MySQL database with C# using Entity framework.
Diether Jay DenampoPosted Mar 7, 2020, 2:56 AM
Hey guys I have a question
Alberto VillarPosted Dec 21, 2019, 6:04 AM
Hello, Please remember to either use parametrized queries or escape strings, specially when they come from user input fields. For example, if I update my name to "Hello'; truncate table student.studentinfo; --" ...
Xavi BarriPosted Oct 19, 2019, 1:59 PM
One suggestion: you're skipping one c# basic naming convention, you're naming variables starting with upper case, they should start with a lower case so it's not confused with objects.
zoubeir sitecPosted Aug 10, 2018, 7:26 AM
Hello, I want to display in a console instead of datagrid view
Sean SimmsPosted Jul 7, 2018, 9:54 PM
This is Awesome Thanks Man!!
junior assouPosted Jun 7, 2018, 6:16 AM
Thnk thank you
Ben 30ixPosted Apr 19, 2018, 7:54 PM
Thanks for the tutorial and the source code bro... You saved me alot
nel2017trash worldPosted Feb 6, 2018, 8:53 PM
Tnx for this is a great help.. tnx again. ;)
Peter EmaPosted Sep 5, 2017, 11:48 AM
Is it possible to make a connection string like that without the username and password?
Ahsan SohailPosted Aug 22, 2017, 3:00 AM
ExecuteReader in your update query :o
Ahsan SohailPosted Aug 22, 2017, 2:59 AM
Although the ExecuteNonQuery returns no rows, any output parameters or return values mapped to parameters are populated with data. For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command.
malik shahzaibPosted Jun 22, 2017, 5:14 PM
If data is not present then ??
Jason DouglasPosted Apr 14, 2017, 10:29 AM
I recommend to take a look at this connector https://www.devart.com/dotconnect/mysql/ It has a lot advanced features like EF support
Bilal HPosted Apr 6, 2017, 3:08 PM
Never use plain text queries, Always use Parameter Queries.
morales tomasPosted Mar 3, 2017, 2:28 PM
Thanks for the example,
ДяДя КоЛяPosted Feb 11, 2017, 4:08 PM
I think somehow so MessageBox.Show(dataGridView1.CurrentRow.Cells[15].Value.ToString());
kishan paneriPosted Jun 15, 2016, 9:01 AM
this article is good but i think one thing is missing that one web it can't show get data from selected row and put into respective textbox.
kamanzi abubakarPosted Feb 12, 2016, 2:58 AM
please send the DB files.
kamanzi abubakarPosted Feb 12, 2016, 2:58 AM
Can you please send the DB file. this is my email. [email protected]
Aishah khanPosted Nov 6, 2015, 3:38 AM
I am using service based Database of visual studio 2013. i used your insert data code but it is not inserting. it shows saved data message box but in database it is not inserting. Can you please help me?
Dina MagdyPosted May 19, 2015, 2:06 AM
can u send the DB files plz ?
nadeem notePosted Jan 18, 2015, 7:09 PM
Could you please tell us how to use data string when using SQL server. I am using SQL Server and trying to create database using c# as frontend. please help. thank you
Supun ManaramPosted Jan 10, 2015, 4:02 PM
Nice tutorial. BTW there are no such db file in your attached file. If you could upload those db file it might be very thankful. :)
Arslan VirkPosted Dec 23, 2014, 4:16 PM
code is defficult ,it should be done using store procedure
primatneuman .Posted Dec 15, 2014, 3:18 AM
Hi! Thank you for the article! What if I need to fill a datagrid using more mysql tables as a source? Where to start?
Ehtesham MehmoodPosted Nov 23, 2014, 5:48 PM
db file is already attached in zip file Muhammad Usman
Ehtesham MehmoodPosted Nov 23, 2014, 5:47 PM
Hania Malik Thank u :)
Hania MalikPosted Nov 19, 2014, 8:19 PM
very nice artical
Code DnaPosted Sep 26, 2014, 12:09 PM
make it downword compatible(must work in vs 2010) and also give the db file.
Nishan WeerasinghePosted Jul 2, 2014, 11:58 PM
Can you put mysql table structure? Thanks.