views:

191

answers:

4

Hi


SET NOCOUNT ON stops the message that shows the count of the number of rows affected by a Transact-SQL statement or stored procedure from being returned as part of the result set.


a) How can you read these messages using C# and ADO.NET ( I assume C# code reading these messages is the same regardless of whether T-SQL statements were executed in a stored procedure, batch or… )?


b) Assuming stored procedure contains several statements, how can your C# code identify to what SQL statement does particular messages refer to?


Thank you

+2  A: 

Take a look at this question and answers. You can't do (b) above without adding some code in your TSQL that captures the @@rowcount and outputs it in some manner (like a resultset that you could read from).

chadhoc
+1  A: 

One option is in your stored procedure is to include variables that you will pass back statement counts. You can do by creating your procedure with the needed OUTPUT parameters.

FIRST SQL HERE
@FirstSQLCount = @@ROWCOUNT

SECOND SQL HERE
@SecondSQLCount = @@ROWCOUNT
StarShip3000
+3  A: 

Informational messages (like the rows affected count info) are reported in ADO.Net through the SqlConnection.InfoMessage event. Add a delegate to the event and will be invoked whenever the server transmits an informational message (ie. any error message with severity bellow 10).

there is no way to associate informational messages like afffected count info with the source. You're going to have to do it based on knowledge of the logic and understand that the first message refers to the first update, the second message to the second update etc.

Relying on affected rows count in the client is generaly a bad practice. The many issues ORM layers like NHibernate and ADO.Net datasets have when SET NOCOUNT ON is turned on just shows how problematic this practice is.

Remus Rusanu
a) What kind of issues does ADO.NEt have with SET NOCOUNT ON?b) So I shouldn't use SqlConnection.InfoMessage event handler to figure out the affected rows count, but using @@rowcount is OK?
carewithl
See http://stackoverflow.com/questions/1483732/set-nocount-on-usage. Using @@ROWCOUNT means is always used in the T-SQL procedure, so it does not have the issues ADO.Net usage of affected count messages has. Read on about the issues ADO.NEt has around SET NOCOUNT ON and make the judgement call in your specific case. Your use may be perfectly safe.
Remus Rusanu
Just to be clear: the ADO.Net issues around SET NOCOUNT ON derive from the ADO.Net relyince on the *informational* message to make decissions about the *correctness* of the operation for concurrency control (determine if the updates were succesfull or not). that is a bad practice, not the use of inromational message per se. These messages are perfectly safe to use for example as user feedback.
Remus Rusanu
thank you all for your help
carewithl
+3  A: 

Don't rely on it. Best practice is SET NOCOUNT ON (discussed with my question here)

  • When you load your datatable, use .Count.
  • Use an OUTPUT parameter to pass @@ROWCOUNT back (or as a dataset)
gbn