检查MySql数据库连接

时间:2013-06-19 15:25:33

标签: c# mysql database-connection

我想为我的程序创建一个mysql连接检查程序,并且我一直使用我常用的顾问SO,然后我登陆Question并且答案不完全正确。

问题是: 如何在c#中检查有效的mysql连接?

1 个答案:

答案 0 :(得分:6)

这是我自己的版本来检查mysql连接。

public static bool checkDB_Conn()
{
    var conn_info = "Server=address;Port=3306;Database=dbhere;Uid=admin;Pwd=123";
    bool isConn = false;
    MySqlConnection conn = null;
    try
    {
        conn = new MySqlConnection(conn_info);
        conn.Open();
        isConn = true;
    }
    catch (ArgumentException a_ex)
    {
        /*
        Console.WriteLine("Check the Connection String.");
        Console.WriteLine(a_ex.Message);
        Console.WriteLine(a_ex.ToString());
        */
    }
    catch (MySqlException ex)
    {
        /*string sqlErrorMessage = "Message: " + ex.Message + "\n" +
        "Source: " + ex.Source + "\n" +
        "Number: " + ex.Number;
        Console.WriteLine(sqlErrorMessage);
        */
        isConn = false;
        switch (ex.Number)
        {
            //http://dev.mysql.com/doc/refman/5.0/en/error-messages-server.html
            case 1042: // Unable to connect to any of the specified MySQL hosts (Check Server,Port)
                break;
            case 0: // Access denied (Check DB name,username,password)
                break;
            default:
                break;
        }
    }
    finally
    {
        if (conn.State == ConnectionState.Open)
        {
            conn.Close();
        }
    }
    return isConn;
}