Skip to content Skip to sidebar Skip to footer

Set Nocount On And Reading Messages Using C# And Ado.net

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 se

Solution 1:

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.

Solution 2:

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)

Solution 3:

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).

Solution 4:

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.

FIRSTSQL HERE
@FirstSQLCount= @@ROWCOUNTSECONDSQL HERE
@SecondSQLCount= @@ROWCOUNT

Post a Comment for "Set Nocount On And Reading Messages Using C# And Ado.net"