Sie sind auf Seite 1von 8

Insert, Update, Delete, Display Data in MySQL Using

C# Ask a Question

Ehtesham Mehmood Feb 26 2014 Article

22 26 1.1m

InsertUpdateDeleteDisplayinMysql.rar | InsertUpdateDeleteDisplayinMysqlCsharp.zip
Easily Add PDF Word & Excel Function to Your .NET Apps

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.

01. using MySql.Data.MySqlClient; //Its for MySQL Ask a Question

Insert Data

01. private void button1_Click(object sender, EventArgs e)


02. {
03. try
04. {
05. //This is my connection string i have assigned the database file address path
06. string MyConnection2 = "datasource=localhost;port=3307;username=root;password=root";
07. //This is my insert query in which i am taking input from the user through windows for
08. string Query = "insert into student.studentinfo(idStudentInfo,Name,Father_Name,Age,Sem
09. //This is MySqlConnection here i have created the object and pass my connection strin
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 dat
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

01. private void button2_Click(object sender, EventArgs e)


In Focus {
02.
 
03.C# Corner
try
COMMUNITY:
04. How to properly ask a question on Foru
{
05. //This is my connection string i have assigned the database file address path
06. Contribute
string MyConnection2 = "datasource=localhost;port=3307;username=root;password=root";
07. //This is my update query in which i am taking input from the user through windows forms a
08. string Query = "update student.studentinfo set idStudentInfo='" + this.IdTextBox.Text + "'
09. //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

01. private void button3_Click(object sender, EventArgs e)


02. { Ask a Question
03. try
04. {
05. string MyConnection2 = "datasource=localhost;port=3307;username=root;password=root";
06. string Query = "delete from student.studentinfo where idStudentInfo='" + this.IdTextBox.Te
07. MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
08. MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2);
09. 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

01. private void button4_Click(object sender, EventArgs e)


02. {
03. try
04. {
05. string MyConnection2 = "datasource=localhost;port=3307;username=root;password=root";
06. //Display query
07. string Query = "select * from student.studentinfo;";
08. MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
09. 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
17. // MyConn2.Close();
18. }
19. catch (Exception ex)
20. {
21. MessageBox.Show(ex.Message);
22. }
23. }

Diagram 2
Ask a Question

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.

C# Insert data in MySQL using C# MySQL Database

Ehtesham Mehmood

Hello guys I am Computer Science graduate from University of Gujrat, Pakistan. I am an


Entrepreneur , i have opened my own software house named
as https://www.facebook.com/MTechnologiesPak in Pakistan. I am a s... Read more
http://www.c-sharpcorner.com/members/ehtesham-mehmood

394 3.6m 1

View Previous Comments


22 26

Type your comment here and press Enter Key (Minimum 18 characters)

Is it possible to make a connection string like that without the username and password?
Peter Ema Sep 05, 2017
1513 2 88 0 0 Reply

ExecuteReader in your update query :o


Ahsan Sohail Aug 22, 2017
1513 2 0 0 0 Reply

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 a ected by the command.
Ahsan Sohail Aug 22, 2017
1513 2 0 0 0 Reply

If data is not present then ?? Ask a Question


malik shahzaib Jun 22, 2017
1512 3 0 0 0 Reply

I recommend to take a look at this connector https://www.devart.com/dotconnect/mysql/ It has a


lot advanced features like EF support
Jason Douglas Apr 14, 2017
1511 4 0 0 0 Reply

Never use plain text queries, Always use Parameter Queries.


Bilal H Apr 06, 2017
1514 1 0 1 0 Reply

Thanks for the example,


morales tomas Mar 03, 2017
1513 2 0 0 0 Reply

I think somehow so MessageBox.Show(dataGridView1.CurrentRow.Cells[15].Value.ToString());


ДяДя КоЛя Feb 11, 2017
1514 1 0 0 0 Reply

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.
kishan paneri Jun 15, 2016
1483 32 1 0 0 Reply

please send the DB les.


kamanzi abubakar Feb 12, 2016
1505 10 0 0 1 Reply

This may help you string Query = "delete from SKILL where Std_ID='" + this.textBox1.Text
+"';";
malik shahzaib Jun 22, 2017
1512 3 0 0

Comment Using
7 Comments Sort by Oldest

Ask a Question
Add a comment...

Ehtesham Shami · Web Application Developer at Ejuicy Solutions


Here I updated the source code with DB files. you can download from here.

http://go2code.com/.../insert-update-delete-display-data.../
Like · Reply · 1 · 1y

Mustapha Ismail Ademola · Agripreneuer at IITA till date


Thank you
Like · Reply · 1 · 1y

Habib Ullah · Abdul Wali khan University Mardan (AWKUM)


this code error to me
Like · Reply · 1y

Keith Douglas · Application Test Enginer at UPC Broadband


Why did you not use odbc? And should you not use ExecuteNonQuery for the record insert and
update rather than executing the query using a reader? Also,what about using using?
Like · Reply · 1y

Michelangelo Machado · UnB


Here's a best solution for deleting:

MySqlConnection conexaoDB = new


MySqlConnection("datasource=localhost;port=3306;username=root;password=root");

connectionDB.Open();

MyCommand = new MySqlCommand("DELETE FROM <dbsource>.<user> WHERE <someID>


= " + <someID>, connectionDB);
MyCommand.ExecuteNonQuery();

connectionDB.Close();
Like · Reply · 30w

Load 2 more comments

Facebook Comments Plugin


Ask a Question

File APIs for .NET


Aspose are the market leader of .NET APIs for le business formats – natively work with
DOCX, XLSX, PPT, PDF, MSG, MPP, images formats and many more!

TRENDING UP

01 Getting Started With Q# Programming

02 C# 8.0 - Experimenting With Non-Nullable Reference Type Using Visual Studio 2017

03 List Of Users With Roles In ASP.NET MVC Identity

04 Angular From Basic To Expert - Day Three

05 Using DataTables Grid With ASP.NET MVC

06 Do You Need A Blockchain

07 Create Site Collection In SharePoint Online Using PowerShell

08 Cyber Security - A Big Opportunity For Developers

09 Using Gijgo Grid With ASP.NET MVC

10 Intelligent Image Object Detection Bot Using Cognitive Computer Vision API
View All
Ask a Question

Philadelphia
New York
London
Delhi

Join C# Corner
and millions of developer friends worldwide.

Enter your email address Sign Up

Learn ASP.NET MVC Learn ASP.NET Core Learn Python Learn JavaScript Learn Xamarin
Learn Oracle More...
Home Events Consultants Jobs Career Advice Stories Partners

About Us Contact Us Privacy Policy Terms Media Kit Sitemap Report a Bug FAQ
©2018 C# Corner. All contents are copyright of their authors.

Das könnte Ihnen auch gefallen