【发布时间】:2014-04-29 10:21:27
【问题描述】:
我正在连接数据库,但在使用无效凭据对其进行测试时出现异常。 连接的代码在一个单独的类中,我已经在那里发现了错误。
但它似乎经历了那个并且有另一个异常指向我调用构造函数的位置,该构造函数位于我的 mainWindow.xaml.cs 中。我该如何处理第二个异常。我在我的 MainWindow 页面上再次尝试/捕获,异常仍然存在。请指教。
//Code at class SimpleDataSource which does the connection:
public void Connect(string server, string database, int port, string user, string password)
{
// TODO: Initialise MySqlConnection object with parameters,
// open connection with suitable exception handling.
string connStr = "server=" + server + ";database=" + database + ";port=" + port + ";user=" + user + ";password=" + password + ";";
try
{
conn = new MySqlConnection(connStr);
conn.Open();
}
catch (MySqlException e)
{
MessageBox.Show(@"Unable to connect to database.
Please check the following and try again.
1. Ensure you have an internet connection.
2. Ensure your credentials are entered correctly");
Console.WriteLine("The error is " + e);
}
}
//Calling the constructor for that class on my MainWindow which is causing the exception:
SimpleDataSource dataSource;
try { dataSource = new SimpleDataSource("111.111.11.11", "eeeee", 3306, "eeeee", "eeee"); }
catch (MySqlException e) { e.StackTrace.ToString();}
错误信息: 我试图换成 XamlParseException 而不是 MySqlException 和相同的结果。
【问题讨论】:
-
在您的异常对话框中,单击
Viewdetail以检查出现错误的原因。当某些 xaml 文件使用了不正确的 xaml 标记或属性值时,此异常 (XamlParseException) 在应用程序加载时出现。您需要确保您的 xaml 文件具有正确的语法。 -
@VS1 仔细检查,你的意思是我的错误是在我的 MainWindow.xaml 页面本身而不是我的类文件?
-
是的@Trevor_zam,它可能在您的
MainWindow.xaml文件中,根据错误对话框检查您的xaml 文件的第3 行和第9 列。另请参阅:msdn.microsoft.com/en-us/library/…。另外,您是否在 MainWindow.xaml 文件的构造函数中调用了其他类? -
不,SimpleDataSource 是我调用的唯一类。这是我的 xaml 第 3 行 - xmlns:x="schemas.microsoft.com/winfx/2006/xaml"。
-
它可以使用正确的凭据。