On the server side, send a message to the client using the RAISERROR function with a severity of 10 (a severity above 10 throws an exception that interrupts the procedure, i.e. passes the execution to the CATCH block, if any). In the following example, I did not add an error number, so the default error number of 50000 will be used by the RAISERROR function. Here is an example:
DECLARE @count INT = 0 DECLARE @infoMessage VARCHAR(1000) = '' -- INSERT SET @count = @@ROWCOUNT SET @infoMessage = 'Number of rows affected ' + CAST(@count AS VARCHAR(10)) RAISERROR(@infoMessage, 10, 0) WITH NOWAIT -- another INSERT SET @count = @@ROWCOUNT SET @infoMessage = 'Number of rows affected ' + CAST(@count AS VARCHAR(10)) RAISERROR(@infoMessage, 10, 0) WITH NOWAIT
On the client side, install the appropriate event handlers, here is an example:
using (SqlConnection conn = new SqlConnection(...)) { conn.FireInfoMessageEventOnUserErrors = true; conn.InfoMessage += new SqlInfoMessageEventHandler(conn_InfoMessage); using (SqlCommand comm = new SqlCommand("dbo.sp1", conn) { CommandType = CommandType.StoredProcedure }) { conn.Open(); comm.ExecuteNonQuery(); } } static void conn_InfoMessage(object sender, SqlInfoMessageEventArgs e) {
source share