Entity Framework 4.2 : Getting proper database errors

不打扰是莪最后的温柔 提交于 2019-11-29 15:47:29

You could do something like this to look for an inner exception that is a SqlException and then handle the sql exception differently.

catch(Exception ex)
{
    Exception current = ex;
    SqlException se = null;
    do
    {
        se = current.InnerException as SqlException;
        current = current.InnerException;
    }
    while (current != null && se == null);

    if (se != null)
    {
        // Do your SqlException processing here
    }
    else
    {
        // Do other exception processing here
    }
}

One could also use the GetBaseException() method as that would get you the root cause exception which is the SqlException.

To get the innermost Exception, you could do something like this:

SqlException se = null;
Exception next = ex;

while (next.InnerException != null) 
{
   se = next.InnerException as SqlException;
   next = next.InnerException;
}

if (se != null)
{
    // Do your SqlException processing here
}
else
{
    // Do other exception processing here
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!