Is this good enough ? do I need to add anything or remove anything ? like a rollback to my Sql Queries ? add catch() ? should my function accept only the property that I need or the object it self ? and how can I let the presentation layer know that the function’s code was executed with no errors .. should I make it book instead of void or what ?
public static void DeleteAllCabinFeaturesFromACruise(int CruiseID)
{
string commandText = "DELETE FROM Cruise_Features WHERE CruiseID = @cruiseId";
SqlConnection connection = new SqlConnection(ConnectionString);
SqlCommand command = new SqlCommand(commandText, connection);
try
{
using (connection)
{
using (command)
{
command.Parameters.AddWithValue("@cruiseId", CruiseID);
connection.Open();
command.ExecuteScalar();
}
}
}
finally { connection.Close(); }
}
You are not using
usingcorrectly. The idea ofusingis to wrap some resource, that needs to be released in a safety-west, that protects it from exceptions. So, the correct way of usingusing(ha-ha) is the following:The second issue is that you are executing your query as if it should return Scalar value. It’s OK, but I think it’s better use just
Execute:command.Execute();And, if you want some error handling, you’d better wrapin a
try ... catchblock like you have. Like this: