存储过程
ALTER procedure [work].[update_status]
@item_id int,
@next_status varchar(60)
as begin
declare
@status varchar(60),
@last_status_date datetime2;
set @status = (select [status] from work.items where id = @item_id); --set status to current status
--Check validity of status change
if not exists (select * from work.status_precedence where status = @status and next_status = @next_status)
begin
declare @err varchar(255) = 'Changes not applied: ' + @next_status + ' is not a valid state following ' + @status + '. See work.status_precedence for valid status advancements.';
raiserror(@err,0,-1);
return -1;
end
--Get date time of last status update
set @last_status_date = isnull(
( select max(added)
from work.status_changes
where item_id = @item_id
),
( select requested
from work.items
where id = @item_id
));
--If request has not been reviewed.
if @status = 'Pending Review' and not exists (select * from work.request_reviews where item_id = @item_id and approval = 1 and reviewed > @last_status_date)
begin
raiserror(N'Changes not applied: Request has not been reviewed since set to pending status.',0,-1);
return -1;
end
--If developer has not been assigned.
if @status = 'Request Reviewed' and not exists (select * from work.assignments where item_id = @item_id and assigned > @last_status_date)
begin
raiserror(N'Changes not applied: Developer has not been assigned.',0,-1);
return -1;
end
--If level of effort is not assigned.
if @status = 'Assigned' and not exists (select * from work.developer_review where item_id = @item_id and added > @last_status_date)
begin
raiserror(N'Changes not applied: Level of effort has not been assigned.',0,-1);
return -1;
end
--If work hasn't been peer reviewed.
if @status = 'Peer Review' and @next_status = 'Business Review' and not exists (select * from work.peer_reviews where item_id = @item_id and approval = 1 and reviewed > @last_status_date)
begin
raiserror(N'Changes not applied: No approved peer review after work item last set to Peer Review status.',0,-1);
return -1;
end
--If work hasn't completed business review.
if @status = 'Business Review' and @next_status = 'Complete' and not exists (select * from work.business_reviews where item_id = @item_id and approval = 1 and reviewed > @last_status_date)
begin
raiserror(N'Changes not applied: No approved business review after work item last set to Business Review status.',0,-1);
return -1;
end
--Log status.
insert into work.status_changes (
item_id,
previous_status,
new_status
) values (
@item_id,
@status,
@next_status
);
--Set current status.
update work.items set status = @next_status
where id = @item_id;
return 0;
end;
VB.NET代码背后
try
command.connection= connection
command.commandtype = storedprocedure
command.commandtext = "work.update_status"
command.parameters.addwithvalue("item_id",value)
connection.open()
command.executenonquery()
catch ex as exception
msgbox(ex.message)
end try
我想从后面的代码中获取raise错误消息,但它不会抛出异常并且永远不会遇到catch块。我在网上找到建议来改变raiseerror的错误严重性,但我没有拥有存储过程,所以我无法更改它。谢谢!
答案 0 :(得分:1)
要捕获严重性为10或更低的邮件,请为连接添加InfoMessage
(https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.infomessage(v=vs.110).aspx)事件的处理程序。 C#示例:
connection.InfoMessage += connection_InfoMessage;
static void connection_InfoMessage(object sender, SqlInfoMessageEventArgs e)
{
MessageeBox.Show(e.Message);
}