Removing a row
A delete removes a row. Because it is irreversible, it deserves more care than any other operation.
The command
C#
using (var conn = new SqlConnection(connString))
using (var cmd = new SqlCommand(
"DELETE FROM Product WHERE Id = @id", conn))
{
cmd.Parameters.AddWithValue("@id", productId);
conn.Open();
int removed = cmd.ExecuteNonQuery();
statusLabel.Text = removed + " row removed";
}Protect the visitor
- Ask for confirmation before the delete runs.
- Delete by identifier, never by a loose condition.
- Consider a soft delete, where a row is marked as inactive instead of being removed, when the data matters.
A delete without a condition empties the table. Read your where clause twice before you run it, and test on a copy of the data first.
That completes the four data operations. To bind results without hand written loops, continue to data controls in practice.