中捕获存储过程打印输出

中捕获存储过程打印输出

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

问题描述

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

Is it possible to capture print output from a T-SQL stored procedure in .NET?

我有很多使用打印作为 errorMessaging 手段的遗留过程.例如,是否可以从以下 PROC 访问输出单词"?

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 = ???

推荐答案

您可以通过将事件处理程序添加到 InfoMessage 连接上的事件.

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 中捕获存储过程打印输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 05:11