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.InfoMessage
manually -- however, for error code specifically, what you really want isTHROW
/RAISERROR
so you get an actualSqlException
.PRINT
messages 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 Mostert Jan 14 '20 at 16:24InfoMessage
(SSMS etc just do the exact same thing internally, and present it as though there is a console) – Marc Gravell♦ Jan 14 '20 at 16:36InfoMessage
) 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 insteadSqlConnection
specific (Dapper tries to be connection-agnostic as far as possible). But: just subscribe toInfoMessage
in your code, and you should be set! – Marc Gravell♦ Jan 14 '20 at 16:39