如何将Oracle异常消息与字符串进行比较?

时间:2016-12-23 19:05:05

标签: c# asp.net oracle

我是C#的新手,我正在努力学习非常基本的东西...例如,我现在正在尝试比较一个我知道将会打印短语的异常" ORA-28007:密码不能重复使用"如果我使用Response.Write(ex.Message)。但是,在下面的块中,ex.Message和刚才提供的字符串之间的比较失败,它返回了未处理的异常我已经放入了else子句......我应该如何将异常与字符串进行比较? / p>

catch (Exception ex)
{
   if (ex.Message == "ORA-28007: the password cannot be reused")
   {
     Response.Write(ex.Message);
     // TODO : Correct the exception to be presented in the popup instead of the same page.
     // display = "The password cannot be reused! Pick a new one.";
     // ClientScript.RegisterStartupScript(this.GetType(), 
     //     "Error.", "alert('" + display + "');", true);
   }
   else
   {
     Response.Write("Unhandled exception: " + ex.Message);
   }
}

1 个答案:

答案 0 :(得分:7)

如果您使用Oracle Data Provider for .NET,则可以通过查看OracleException属性获取Exception而不是Errors s并获取更多详细信息OracleError个对象列表:

catch(OracleException oex)
{
    foreach (OracleError error in oex.Errors) 
    {
        Console.WriteLine("Error Message: " + error.Message);
        Console.WriteLine("Error Source: " + error.Source);

        if(error.Number == 28007) 
        {
            // do specific stuff
        }       
     }
 }