使用SqlCommand的执行非查询,你可以得到的文本通常贴到"信息"? [英] Using SqlCommand to execute a non-query, can you get to text normally posted to "Messages"?

查看:148
本文介绍了使用SqlCommand的执行非查询,你可以得到的文本通常贴到"信息"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用的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.

马克·

这篇关于使用SqlCommand的执行非查询,你可以得到的文本通常贴到"信息"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆