I was wondering if there is a better way to code this:
using (SqlConnection connection = new SqlConnection(ConnectionString)) {
using (SqlCommand cmdDelete = new SqlCommand("DELETE FROM table WHERE
(TestNumber = '" + testNumber + "')", connection)) {
try
{
connection.Open();
int recordExists = deletePolicyFromTdm.ExecuteNonQuery();
connection.Close();
if(recordExists == 1)
result.Append("Record has been deleted");
else
result.Append("Record does not exist");
...
Couple of assumptions:
testNumber
is unique and no duplicate records and was input tested, at least according to bus rules. So I realize that if more than one record is present theif
statement won't work properly- Table has a PK but
testNumber
isn't it - Only one roundtrip to db
- Can't change how DB is set up
- SQL command will eventually be replaced with a sproc and parameter
The idea is that if user clicks a button and gets either a message that no record exists or that the records were deleted.