Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
Whenever we execute an insert, update, delete query or stored procedure we get a message in sql server something like.. "5 rows affected" or an error message or a sql PRINT message. I curious how can i get this message through ADO.NET if its at all possible. Thanks.
Posted
Updated 22-Aug-13 21:21pm
v2
Comments
Dholakiya Ankit 23-Aug-13 3:36am    
what have you done first?
Thanks7872 23-Aug-13 3:46am    
This question seems illogical to me as you are inserting/deleting/updating something than you already know what you have changed. You can just grab that changes based on your choice like the number of records updated etc.

SqlCommand.ExecuteNonQuery() always returns the number of rows affected by the command.

check this: SqlCommand.ExecuteNonQuery Method [^]

RETURN @@ROWCOUNT from the stored procedure returns the number of rows affected

check this: Modifying Data with Stored Procedures[^]

RETURN @@ERROR from stored procedure will return the error

check this: @@ERROR (Transact-SQL)[^]
 
Share this answer
 
v2
Ly try this:
C#
SqlConnection connection = new SqlConnection("server=.;database=Northwind;integrated Security=SSPI;");

connection .InfoMessage += new SqlInfoMessageEventHandler(InfoMessageHandler);

static void InfoMessageHandler(object sender, SqlInfoMessageEventArgs e)
{
    string myMsg = e.Message;            
}

The "e.Message" is the message printed out to the message window in SQL Server Management Studio.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900