ado.net

With ADO.NET, how do I capture the messages from a stored procedure call?


When I run a stored proc in SSMS, it dumps a lot of diagnostic information using PRINT commands that I can see in the messages tag.

Using C#, how would I capture that output?

NOTE: I am not asking how to print statements in T-SQL.


Solution

  • I believe you can tap into the SqlConnection.InfoMessage event in the Microsoft.Data.SqlClient namespace to get those notifications and inspect them / handle them.

    public event Microsoft.Data.SqlClient.SqlInfoMessageEventHandler InfoMessage;