114

Is it possible to capture print output from a T-SQL 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 = ???
1
  • 6
    It maybe not only about errors. I will try to use this to keep track of progress of a long running stored proc by watching the informative output. Commented Apr 18, 2013 at 17:14

3 Answers 3

163

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)
    {
       Console.WriteLine(e.Message);
    }
Sign up to request clarification or add additional context in comments.

5 Comments

If you also want the rows affected count then you need a handler for the StatementCompleted event on the SqlCommand.
@Nicholas can you elaborate? I can't get that event to work properly. Please see stackoverflow.com/questions/27993049/…
Are you catching all messages produced within sql server with that event ? Is it possible that this event will also catch some other messages, not produced by that stored procedure ?
This may be obvious but if there is no output from the proc (no print, raiseerror, etc.) then the event is not triggered. This stumped me for a while until I realized what was happening.
@FrenkyB I can confirm that it will capture all output (print, raiserror, etc.) from the invoked proc or any procs or functions which it calls.
11

This is really handy if you want to capture Print output in LinqPad's output console:

SqlConnection conn = new SqlConnection(ConnectionString);
//anonymous function to dump print statements to output console
conn.InfoMessage += (object obj, SqlInfoMessageEventArgs e)=>{
                e.Message.Dump();
            };

2 Comments

Where does .Dump() come from? Is this a custom extension method?
@ˈvɔlə - Yes, this is a LinqPad extension method that dumps data to the results pane.
6

To get the output into a variable:

string printOutput = "";

using (var conn = new SqlConnection(...))
{
    // handle this event to receive the print output
    conn.InfoMessage += (object obj, SqlInfoMessageEventArgs e) => {
        printOutput += e.Message;
    };

    // 
    // execute command, etc. here
    // 
}

Console.Write(printOutput);

2 Comments

i like compactness of this
This is kind of a side topic, but repeated string concatenation is a minor code smell for some folks. Appending to a List<string> and doing a string.Join() at the end is usually preferable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.