In your stored procedure, you can request @@ ROWCOUNT , which will give you the affected records. Now you can save this in a variable using the SET or SELECT , for example
SET MyRecordCount = @@RowCount
or
SELECT MyRecordCount = @@RowCount
Alternatively, if you have several operations in one procedure that you need to track, you can either create multi-valued variables, or call SET or SELECT several times or use the TABLE variable, for example.
DECLARE @recordCount table (Records int not null)
Where will it insert the value @@ROWCOUNT into the table variable @recordCount
Next, to get this information, you will need to output the last row selection from the @recordCount table.
Finally, in your code, instead of the ExecuteNonQuery() method, you should use a data reader like.
using (var connection = new SqlConnection(connectionString)) { connection.Open(); using (SqlCommand command = new SqlCommand("MyStoredProcedure", connection)) { command.CommandType = CommandType.StoredProcedure; using (var reader = command.ExecuteReader()) { while (reader.Read()) { item.message = reader.GetString(0); } reader.Close(); } } }
Now the message is actually an integer number of lines, which is not affected by the term (98) row affected , but if you really want the exact message to simply format the line as you wish.
item.message = string.Format("({0}) rows affected", reader.GetInt32(0))