开发者

C# and SQL Server: any reason to put a single INSERT into a transaction?

Using C# and SQL Server, would there be any reason to put a single INSERT statement into a transaction?

I am reviewing code writt开发者_开发知识库en by someone else. I can't understand why a transaction would be needed since there is only one SQL statement.


It will already be in an implicit transaction. No need to wrap it in another redundant transaction.


No reason at all.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜