views:

161

answers:

1

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 = ???
+7  A: 

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);
}
AdaTheDev
Works like a charm! Thanks man
Peter