I have a stored procedure which I can't modify and I'm trying to get the PRINT message that get's returned if an error occurs. The code in my stored procedure is.
IF((ISNULL(@ID,''))='')
BEGIN
PRINT N'Error: This ID is invalid.';
RETURN;
END
In my dapper call, I have the following:
var storedProcResult = connection.Query("AdventureWorks.dbo.myStoredProcedure", new
{
@ID = null
},
commandType: CommandType.StoredProcedure
);
The value of storedProcResult is 0 but I'm interested getting the PRINT message. Please advise.
SqlConnection.InfoMessagemanually -- however, for error code specifically, what you really want isTHROW/RAISERRORso you get an actualSqlException.PRINTmessages are very easy to ignore by clients, and in fact the vast majority of them do ignore them. Did whoever wrote this procedure live in a land where only Management Studio was used for all work, and nobody ever ignored the "Messages" tab? Seems like something that's not viable in the long run. - Jeroen MostertInfoMessage(SSMS etc just do the exact same thing internally, and present it as though there is a console) - Marc GravellInfoMessage) is at the connection level, not the command, and Dapper is mostly interested in commands as the unit of work. Also: it isn't defined onDbConnection, but is insteadSqlConnectionspecific (Dapper tries to be connection-agnostic as far as possible). But: just subscribe toInfoMessagein your code, and you should be set! - Marc Gravell