Using C# and SQL Server, would there be any reason to put a single INSERT statement into a transaction?
I am reviewing code written by someone else. I can’t understand why a transaction would be needed since there is only one SQL statement.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Programmatically, there would be no reason. The reason might be in readability though; wrapping an arbitrary code snippet in a transaction is a pretty good way of drawing the attention of future developers to it – or away from something else.
It might also just be that the Best Practice at the time of writing was to wrap any dealings with the database in a transaction, just as wrapping stuff in try/catch-blocks.
My suggestion: If the code is clearer and more readable because of this, or if all other database calls are in transactions, leave it in. Otherwise, remove it.