Database Error Handling in Entity Infrastructure

I wrote a stored procedure, sometimes RAISERROR () . I execute it through the entity infrastructure, for example:

using( MyModelEntities conn = new MyModelEntities() ) {
    conn.MyStoredProcedure(input_p, output_p);
}

Stored Procedure:

create procedure dbo.MyStoredProcedure(
    @input   nvarchar(255),
    @output int out
)
as
begin
    ...
        RAISERROR (N'My Exception....', 10, 1);
    ...
end
go

Is it possible to get error information?

+5
source share
3 answers

In sql server, exception level 1-10 is informative and does not return an error in your application.

change the level between 11 and 16 to raise the error from SP.

+3
source

Can this just be put in try / catch with the exception thrown? Or maybe you can try switching to debug mode?

+2
source

:

create procedure dbo.MyStoredProcedure(
    @input   nvarchar(255),
    @output  text out
)
as
begin
    ...
      SELECT @message = convert(varchar, getdate(), 108) + ': My Exception....'
      SET @output = CAST(@message AS text) -- send message to c#/.net
      RAISERROR(@message,0,1) WITH NOWAIT  -- send message to SQL Server Management Studio
    ...
end
go

Then on the C # / EF side:

public string CallMyStoredProcedure(string input)
{
    var outputParameter = new ObjectParameter("output", typeof(string));
    EntityContext.MyStoredProcedure(input, outputParameter);
    return (string)outputParameter.Value;
}
0
source

All Articles