小编典典

在.NET中捕获存储过程的打印输出

c#

是否可以从.NET中的T-SQL存储过程捕获打印输出?

我有很多使用打印作为errorMessaging手段的旧式proc。例如,是否可以从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 = ???

阅读 352

收藏
2020-05-19

共1个答案

小编典典

您可以通过在连接上的InfoMessage事件中添加事件处理程序来实现。

myConnection.InfoMessage += new SqlInfoMessageEventHandler(myConnection_InfoMessage);

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