UPDATE TableName
SET ColumnName = Expression
With this formula, you must specify the name of the
involved table as the TableName factor of our formula. The SET
statement allows you to specify a new value, Expression, for the
field under the ColumnName column.
In SQL, the primary formula of the UPDATE statement as introduced
on our formula can be used to update all records. Here is an example:
void btnUpdateAllRecords_Click(object sender, EventArgs e)
{
SqlConnection connection =
new SqlConnection("Data Source=(local);" +
"Database='Exercise';" +
"Integrated Security=yes;");
SqlCommand command = new SqlCommand("UPDATE Employees " +
"SET HourlySalary = 20.00;", connection);
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
When this code executes, the hourly salary of all employees on the table will
be set to 20.00
To edit a record, you must provide a way
to locate the record. To do this, you use the WHERE operator
in an UPDATE statement using the following formula:
UPDATE TableName
SET ColumnName = Expression
WHERE Condition(s)
The WHERE operator allows you to specify how the
particular record involved would be identified. Here is an example:
void btnUpdateRecord_Click(object sender, EventArgs e)
{
SqlConnection connection =
new SqlConnection("Data Source=(local);" +
"Database='Exercise';" +
"Integrated Security=yes;");
SqlCommand command = new SqlCommand("UPDATE Employees " +
"SET EmployeeName = 'Henry Helmonds' " +
"WHERE EmployeeNumber = '72-644';",
connection);
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}