Using SqlCommand to execute a non-query, can you get to text normally posted to “Messages”?

后端 未结 1 2121
难免孤独
难免孤独 2021-02-19 05:50

When using SqlCommand to execute a non-query (such as a database restore), is it possible to programatically get the text that would normally be posted to the \"Messages\" tab i

相关标签:
1条回答
  • 2021-02-19 06:17

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

    SqlConnection _con = new SqlConnection("server=.;database=Northwindintegrated Security=SSPI;");
    
    _con.InfoMessage += new SqlInfoMessageEventHandler(_con_InfoMessage);
    

    The event handler will look like this:

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

    The "e.Message" is the message printed out to the message window in SQL Server Mgmt Studio.

    Marc

    0 讨论(0)
提交回复
热议问题