Access to SQL Server messages via ADO.NET

后端 未结 3 1702
伪装坚强ぢ
伪装坚强ぢ 2020-12-10 10:59

Is it possible to access the SQL Server \"by-product messages\" via ADO.NET? Due to the lack of words, by \"by-product messages\" I mean the output which appears in the Mess

3条回答
  •  忘掉有多难
    2020-12-10 11:16

    Yes, there's an event on the SqlConnection class called SqlInfoMessage, which you can hook into:

    SqlConnection _con = 
       new SqlConnection("server=.;database=Northwind;integrated Security=SSPI;");
    
    _con.InfoMessage += new SqlInfoMessageEventHandler(InfoMessageHandler);
    

    The event handler will look like this:

    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.

提交回复
热议问题