Databases Reference
In-Depth Information
//
// map parameters
cmd.Parameters.Add(
"@employeeid",
SqlDbType.Int,
4,
"employeeid");
you select the row to delete and delete it. Actually, you select all rows for employees
named Roy Beatty, since you don't know (or care about) their employee IDs. Although
you expect only one row to be selected, you use a loop to delete all the rows. (If you were
to run the PersistAdds program multiple times, you'd have more than one row that
matches this selection criteria.)
// select employees
string filt = @"
firstname = 'Roy'
and
lastname = 'Beatty'
";
//
// delete employees
foreach (DataRow row in dt.Select(filt))
{
row.Delete();
}
Finally, you set the data adapter's DeleteCommand property with the command to
delete from the Employees table so it will be the SQL the data adapter executes when
you call its Update method. You then call Update() on the data adapter to propagate the
changes to the database.
da.DeleteCommand = cmd;
da.Update(ds, "employees");
Whether you delete one row or several, your SQL is parameterized, and the data
adapter will look for all deleted rows in the employees data table and submit deletes for
all of them to the Employees database table.
If you check with Database Explorer or the SSMSE, you'll see the row has been
removed from the database. Sir Roy Beatty is no longer in the Employees table.
Search WWH ::




Custom Search