6

我有时会写存储过程女巫RAISERROR()。我通过实体框架执行它,例如:

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

存储过程:

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

有没有机会获得有关错误的信息?

4

3 回答 3

4

在 sql server 中,异常级别 1 到 10 是信息性的,不会将错误返回给您的应用程序。

在 11 到 16 之间更改 Level 以从 SP 引发错误。

于 2016-06-23T14:14:55.770 回答
2

可以将其放入 try/catch 中并显示异常吗?或者,也许您可​​以尝试进入调试模式?

于 2012-05-01T14:03:55.927 回答
0

这是我的处理方式:

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

然后在 C#/EF 端:

public string CallMyStoredProcedure(string input)
{
    var outputParameter = new ObjectParameter("output", typeof(string));
    EntityContext.MyStoredProcedure(input, outputParameter);
    return (string)outputParameter.Value;
}
于 2013-02-21T20:55:07.977 回答