【发布时间】:2012-12-29 00:56:45
【问题描述】:
我正在尝试捕获重复的密钥违规行为。我可以在 Intellisense 弹出窗口中看到 System.OleDB.OleDBException,但内部异常为空。如何访问 System.OleDB.OleDBException 中的错误代码?
格雷格
try
{
MyData.ConExec(sSQL);
}
catch (Exception ex)
{
OleDbException innerException = ex.InnerException as OleDbException;
if (innerException.ErrorCode == -2147217873)
{
// handle exception here..
}
else
{
throw;
}
}
【问题讨论】:
-
尝试捕获 OleDBException 而不是捕获一般异常。
-
以上代码在
innerException.ErrorCode上的引用可能为空
标签: c# exception try-catch oledb