I have been trying to adapt my code (a webservice) to get the SQLServerMessages:
SqlConnection c = new SqlConnection(ConfigurationManager.ConnectionStrings["db"].ConnectionString);
//capture the infomessage event to capture
c.InfoMessage += delegate(object sender, SqlInfoMessageEventArgs e)
{
serverMessages += e.Message; //"\n" + e.Message
};
SqlDataAdapter dAdapter = new SqlDataAdapter(query, c);
SqlCommandBuilder cBuilder = new SqlCommandBuilder(dAdapter);
DataTable dTable = new DataTable("C");
dAdapter.Fill(dTable);
But while the dTable fills up nicely, the serverMessages-String remains empty (which should return at least 1 line. just like MS SQL Server MMS.) I am obviously missing something important. Any help is appreciated!
The
InfoMessageevent only broadcastsPRINTstatements and low-level errors (severity 10 or lower), not the row count message.To test your code (which looks fine), try adding a
PRINTstatement to your command’s text or to the underlying procedure.If you want to capture row counts, just add something like this after the appropriate statement(s):