我有以下调用存储过程的代码。我希望能够捕获存储过程运行期间发生的任何错误。
try {
using (var connection = GetConnection()) {
using (SqlCommand cmd = connection.CreateCommand()) {
connection.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "VerifyInitialization";
cmd.Parameters.Add(new SqlParameter("@userId", user.Id));
cmd.Parameters.Add(new SqlParameter("@domainId", user.DomainId));
cmd.ExecuteNonQueryAsync();
}
}
}
catch (Exception ex) {
throw new LoginException(LoginExceptionType.Other, ex.Message);
}
这是存储过程,基本上只是调用其他存储过程。
ALTER PROCEDURE [dbo].[VerifyInitialization]
-- Add the parameters for the stored procedure here
@userId int,
@domainId int
AS
BEGIN
Begin Try
SET NOCOUNT ON;
Exec VerifyInitializationOfDefaultLocalizationItems
Exec VerifyInitializationOfLayoutLists @domainId
Exec VerifyInitializationOfLayoutListItems @domainId
Exec VerifyInitializationOfLocalizationItems @domainId
Exec VerifyInitializationOfLookupLists @domainId
Exec VerifyInitializationOfLookupListItems @domainId
End try
Begin Catch
-- Raise an error with the details of the exception
DECLARE
@ErrMsg nvarchar(4000) = Error_message(),
@ErrSeverity int = ERROR_SEVERITY();
RAISERROR(@ErrMsg, @ErrSeverity, 1)
End Catch
End
我需要做什么才能捕获将返回给 C# 的存储过程中的错误?例如,重命名字段名称会阻止其中一个存储的过程运行。我不希望它默默地失败。
格雷格