I have searched all afternoon and don't believe this is a duplicate to the other questions floating around like ExecuteNonQuery() and SET NOCOUNT ON.
I have also found an interesting blog about Finding code smells using SQL Prompt: the SET NOCOUNT problem (PE008 and PE009) which does go into some interesting detail but still doesn't answer my question.
I have a database server where the admins have ticked the no count
option for the server connections (In SSMS, right click the server in the Object Explorer, go to Properties, select the Connections page, under 'Default connection options', scroll down till you find the 'no count' selector).
Based on all the reading I've done, more and more admins may well be ticking the no count
option to increase performance since the resulting rows will no longer be sent back to the client.
My question therefore mainly focuses on the SqlCommand.ExecuteNonQuery Method in C# since it relies on the fact that it:
Executes a Transact-SQL statement against the connection and returns the number of rows affected.
But if SET NOCOUNT ON
is enforced, then the result will always be -1. I've read about how people are recommending rather using select @rowcount = @@ROWCOUNT
but this doesn't make up for the fact that you're losing functionality from the SqlCommand.ExecuteNonQuery
method to the point that you might as well just start using SqlCommand.ExecuteScalar instead.
So my question is, for best practice, should we start setting SQL Servers to no count
(or at the very least expect that's how they're going to start configuring them in the next couple of years) and then if so, should we be forcing SET NOCOUNT OFF
or drop SqlCommand.ExecuteNonQuery
in favour of select @rowcount = @@ROWCOUNT
?