我正在 try catch 重复的 key 违规。我可以在 Intellisense 弹出窗口中看到 System.OleDB.OleDBException,但内部异常为 null。如何访问 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;
}
}
最佳答案
不要声明异常的实例。如果你这样做,它肯定会返回空。
try
{
MyData.ConExec(sSQL);
}
catch (OleDbException ex)
{
// handle excpetion here...
if (ex.ErrorCode == -2147217873)
{
}
}
catch (Exception e)
{
// if other exception will occur
}
关于c# - 捕获具有特定错误代码的 oledb 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14077708/