In this part of the demonstration, we are going to update the Employee item. Therefore, add the following code:
- Copy and paste the UpdateEmployeeItem method after your QueryItems method. Create the connection again, and the query, as follows:
static void UpdateEmployeeItem()
{
connection = new SqlConnection(connectionstring);
using (connection)
{
try
{
Console.WriteLine(" Updating employee:");
Console.WriteLine("========================================= ");
var cmd = new SqlCommand(" UPDATE Employee SET FirstName = 'Molly' WHERE Employeeid = 1", connection);
- Execute it, as follows:
connection.Open();
cmd.ExecuteNonQuery();
connection.Close();
Console.WriteLine(" Finished updating employee");
Console.WriteLine("========================================= ");
}
catch (SqlException e)
{
Console.WriteLine(e.ToString());
}
}
}
- Then, we need to call UpdateEmployeeItem in the GetStartedDemo method again, as follows:
static void GetStartedDemo()
{
connectionstring = "<replace-with-your-connectionstring>";
connection = new SqlConnection(connectionstring);
AddItemsToDatabase();
QueryItems();
//ADD THIS PART TO YOUR CODE
UpdateEmployeeItem();
}
In this part of the demonstration, we updated a row in the database. In the next part, we will delete the employee from the database.