Updating an Azure SQL Database row

In this part of the demonstration, we are going to update the Employee item. Therefore, add the following code:

  1. 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);
  1. Execute it, as follows:
                    connection.Open();
cmd.ExecuteNonQuery();
connection.Close();

Console.WriteLine(" Finished updating employee");
Console.WriteLine("========================================= ");
}
catch (SqlException e)
{
Console.WriteLine(e.ToString());
}
}
}
  1. 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset