使用SqlCommand的执行非查询,你可以得到的文本通常贴到"信息"?你可以、贴到、文本、信息

2023-09-03 06:44:31 作者:深夜_蹲大街

在使用的SqlCommand执行非查询(如数据库恢复),是否有可能以编程方式得到,如果使用Management Studio中通常会被发布到信息选项卡中的文字?如果是的话怎么办?

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 if using the Management Studio? And if so how?

推荐答案

是的,有一个名为SqlInfoMessage SqlCommand对象,您可以连接到一个事件挂钩:

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;            
}

在e.Message是打印出来,在SQL Server管理工作室。

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

马克·