How to Update A MySQL Database with C#

Maintaining a Database by using the MySQL .NET Connector and C#

© Mark Alexander Bain

May 30, 2009
Update a MySQL Database with C#, Mark Alexander Bain
It is important to keep the contents of a database up to date. It's fortunate, therefore, that C# and the MySQL .NET connector make that an easy task.

There are 4 basic kinds of SQL (Structured Query Language) statements that the typical C# programmer is interested in. These are:

  • Insert
  • Select
  • Update
  • Delete

These can be used by the C# programmer to add records to MySQL database, to view them, to delete them and, as will be shown in this article, to modify the contents of the database.

The Update Statement and the Importance of the Where Clause

The update statement is used to modify the details stored in individual records in a database, and has the format:

update table_name
set field1 = ‘new information’

However, this by itself is a dangerous statement because it will update every record in the table. It is, therefore, essential that the “where” clause is always used:

update table_name
set field1 = ‘new information’
where id = 26

And it’s worth noting that more that one field in the record can be updated in a single update statement:

update table_name
set field1 = ‘new information’, field2 = ‘updated too’
where id = 26

It’s also worth noting that the statements are not case sensitive, so the query can be written as:

UPDATE table_name
SET field1 = ‘new information’, field2 = ‘updated too’
WHERE id = 26

However, it is always good practice to change one format and stick to it.

Connecting to a MySQL Database

Whichever format a programmer chooses and regardless of whether they are going to insert, select, update or delete records, they will first need to connect to the MySQL database. Therefore that is covered in a separate article: How to Connect to a MySQL Database with C#. The article shows how to create the MySQL connection that will be required to carry out any database operations.

The Update Statement and C#

The insert statement will be a command run via the connection and so the first step is to create a command object:

MySqlCommand command = connection.CreateCommand ();

The C# application must, of course, determine the information to be update. In this example that’s done as part of a console application (as shown in figure 1 at the bottom of this article):

Console.Write("Enter Sample Number> ");
int sample_no = Convert.ToInt16(Console.ReadLine());

That information can then be used to create the update statement (the table structure used in this example can be seen in figure 2):

command.CommandText =
"update samples"
+ " set processed = now() "
+ " where id = " + sample_no;

And the command can be executed on the database:

MySqlDataReader result = command.ExecuteReader();

If the database is examined at the end of this process then the selected record will have been updated with a current date.


The copyright of the article How to Update A MySQL Database with C# in Database Programming is owned by Mark Alexander Bain. Permission to republish How to Update A MySQL Database with C# in print or online must be granted by the author in writing.


Update a MySQL Database with C#, Mark Alexander Bain
Figure 1: A C# Console Application, Mark Alexander Bain
Figure 2: A Sample MySQL Table, Mark Alexander Bain
   


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo