Databases Reference
In-Depth Information
How It Works
You add an UPDATE statement and change the name of the original query string variable
from sql to upd in order to clearly distinguish it from this statement.
// SQL to update employees
string upd = @"
update employees
set
city = @city
where
employeeid = @employeeid
";
You replace the update comment in the try block with quite a bit of code. Let's look
at it piece by piece. Creating a command is nothing new, but notice that you use the
update SQL variable ( upd ), not the query one ( sql ).
// update Employees
//
// create command
SqlCommand cmd = new SqlCommand(upd, conn);
Then you configure the command parameters. The @city parameter is mapped to
a data column named city. Note that you don't specify the data table, but you must be
sure the type and length are compatible with this column in whatever data table you
eventually use.
// city
cmd.Parameters.Add(
"@city",
SqlDbType.NVarChar,
15,
"city");
Next, you configure the @employeeid parameter, mapping it to a data column named
employeeid. Unlike @city , which by default takes values from the current version of the
data table, you want to make sure that @employeeid gets values from the version before any
changes. Although it doesn't really matter here, since you don't change any employee IDs,
it's a good habit to specify the original version for primary keys, so if they do change, the
correct rows are accessed in the database table. Note also that you save the reference
returned by the Add method so you can set its SourceVersion property. Since you don't
need to do anything else with @city , you don't have to save a reference to it.
Search WWH ::




Custom Search