0

似乎无法捕获 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: 
4

2 回答 2

0

尝试修改您的连接字符串。对于 MySql,基本格式应该是这样的:

服务器=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;

于 2013-04-17T22:31:07.677 回答
0

Definitely a timeout issue. Either wait for the connection to timeout based on the default settings, or explicitly set a timeout of like 5 seconds, and you should be able to catch the exception.

于 2013-04-17T21:38:33.367 回答