捕捉.NET存储过程的打印输出 [英] Capture Stored Procedure print output in .NET

查看:160
本文介绍了捕捉.NET存储过程的打印输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能从.NET中TSQL存储过程捕获的打印输出?

我有很多使用的打印作为errorMessaging手段遗产特效的。一个例子,是有可能从以下PROC访问outprint'字'?

   - 在proc
CREATE PROC usp_PrintWord AS
    PRINT'字'

//一些C#code到想退出'字'
的SqlCommand CMD =新的SqlCommand(usp_printWord,TheConnection);
cmd.CommandType = CommandType.StoredProcedure;
//字符串ProcPrint = ???
 

解决方案

您可以通过添加一个事件处理程序的<一个做到这一点href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.infomessage.aspx">InfoMessage事件的连接上。

  myConnection.InfoMessage + =新SqlInfoMessageEventHandler(myConnection_InfoMessage);

无效myConnection_InfoMessage(对象发件人,SqlInfoMessageEventArgs E)
{
    myStringBuilderDefinedAsClassVariable.AppendLine(e.Message);
}
 

Is it possible to capture print output from a TSQL stored procedure in .NET?

I have a lot of legacy Procs that use the print as means of errorMessaging. An example, is it possible to access the outprint 'word' from following PROC?

-- The PROC
CREATE PROC usp_PrintWord AS
    PRINT 'word'

// Some C# Code to would like to pull out 'word'
SqlCommand cmd = new SqlCommand("usp_printWord", TheConnection);
cmd.CommandType = CommandType.StoredProcedure;
// string ProcPrint = ???

解决方案

You can do this by adding an event handler to the InfoMessage event on the connection.

myConnection.InfoMessage += new SqlInfoMessageEventHandler(myConnection_InfoMessage);

void myConnection_InfoMessage(object sender, SqlInfoMessageEventArgs e)
{
    myStringBuilderDefinedAsClassVariable.AppendLine(e.Message);
}

这篇关于捕捉.NET存储过程的打印输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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