是否可以从.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 = ???
您可以通过在连接上的InfoMessage事件中添加事件处理程序来实现。
myConnection.InfoMessage += new SqlInfoMessageEventHandler(myConnection_InfoMessage); void myConnection_InfoMessage(object sender, SqlInfoMessageEventArgs e) { myStringBuilderDefinedAsClassVariable.AppendLine(e.Message); }