当我运行类似
的东西时BACKUP LOG [somedb]
TO DISK = N'i:\log.bak';
它抛出 2 条错误消息:
Msg 3201, Level 16, State 1, Line 2
Cannot open backup device 'i:\log.bak'. Operating system error 3(The system cannot find the path specified.).
Msg 3013, Level 16, State 1, Line 2
BACKUP LOG is terminating abnormally.
当我尝试使用 TRY CATCH 处理错误时,返回的错误始终是 3013。这对我来说是个问题,因为我想知道备份是否因空间不足而失败,或者驱动器不存在等
使用@@ERROR 返回相同的错误号。
有没有办法处理像这样的多个错误消息?
最佳答案
您需要检查 SqlException
中的 Errors
集合:
catch(SqlException sqlEx)
{
foreach(SqlError error in sqlEx.Errors)
{
int code = error.Number;
string msg = error.Message;
}
}
您应该在 SqlException.Errors
关于sql-server - 如何处理多个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5966670/