c# - 捕获具有特定错误代码的 oledb 异常

标签 c# exception try-catch oledb

我正在 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/

相关文章:

javascript - 在 javascript 中使用的引用在 catch-Block 中不可用

java - 为什么 Java ME 没有异常处理程序?

c# - SortedDictionary 按键的相反顺序

c# - 请求中的错误 SOAP 操作 header 。为什么?

c# - Windows 身份基础系统.Xml.XmlException : Unexpected end of file

c# - 计算处理时间

exception - Dojo - 吞下 Promise 中的 ReferenceError 异常

java-如何创建延迟一定时间的字符串检查?

java - ibatis spring java.lang.NoSuchMethodError com.ibatis.sqlmap.engine.builder.xml.SqlMapConfigParser.parse

Java Servlet NumberFormatException