似乎无法捕获 MySQL 连接器引发的异常。我故意弄错了连接字符串设置,当我点击它时c.Open()
,它停在那里,但有例外。为什么不会被抓?在该块中使用Throw New Exception()
按预期工作。
string cString = string.Format( @"server={0};User Id={1};password={2};Persist Security Info=True;database={3}",
tbMySQLServer.Text, tbMySQLUser.Text, tbMySQLPw.Text, tbMySQLDB.Text );
MySql.Data.MySqlClient.MySqlConnection c = new MySql.Data.MySqlClient.MySqlConnection( cString );;
try
{
c.Open();
tbMySQLTestResult.Text = "Success!";
if ( cString != Properties.Settings.Default.commvetgenboxConnectionString )
{
DialogResult dr = MessageBox.Show( "Would you like to save this new connection string?",
"Save?", MessageBoxButtons.YesNo, MessageBoxIcon.Question );
if ( dr == DialogResult.Yes )
{
Properties.Settings.Default.commvetgenboxConnectionString = cString;
Properties.Settings.Default.Save();
}
}
}
catch ( Exception ex )
{
logger.ErrorException( "Error accessing database", ex );
MessageBox.Show( "Error opening database./r/nSee log for detailed information./r/n" + ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
tbMySQLTestResult.Text = "Error";
}
finally
{
if ( c.State == System.Data.ConnectionState.Open )
{
c.Close();
}
}
编辑:这是我点击时的异常c.Open()
:
MySql.Data.MySqlClient.MySqlException occurred
Message=Access denied for user 'ro'@'localhost' (using password: YES)
Source=MySql.Data
ErrorCode=-2147467259
Number=1045
StackTrace:
at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
InnerException: