如何在.NET应用程序中正确检查与mysql的数据库连接?

时间:2014-07-31 22:02:30

标签: c# mysql database

首先,我想确切地说,我一直在互联网上搜索我的问题的解决方案,我找到的只是经典

if(connection.open) return true; 
在我的案例中,

解决方案无法满足我的需求。

我正在处理一个应用程序。在运行之前,我需要检查与MySQL数据库的连接,如果连接没有完成,则会打开一个新窗口,提示用户进行连接设置(用户名,服务器地址,密码......)。在这个窗口中,我有一个测试连接按钮,看看是否建立了连接,这是button_click事件的代码:

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    ApplicationSettings applicationSettings = new ApplicationSettings();

    applicationSettings.ServerDatabase = tbdbName.Text;
    applicationSettings.ServerIp = tbServer.Text;
    applicationSettings.ServerUserName = tbUsername.Text;
    //applicationSettings.ServerPassword = pbPassword.SecurePassword;
    applicationSettings.MakeConnectionString();
    MySqlConnection connection = new MySqlConnection(applicationSettings.ConnectionString);
    try
    {
        connection.Open();

        MessageBox.Show(this, "connection string: "+applicationSettings.ConnectionString+"connection OK!", "OK !", MessageBoxButton.OK, MessageBoxImage.Information);
    }
    catch (Exception ee)
    {
        MessageBox.Show(this, "connection string : "+applicationSettings.ConnectionString+"error : " + ee.Message, "connection ERROOOOR", MessageBoxButton.OK,
            MessageBoxImage.Error);

    }
    finally
    {
        if(connection.State == ConnectionState.Open)
            connection.Close();
    }

}

如果我只输入服务器地址" applicationSettings.ServerIp"然后单击测试连接按钮,将显示messageBox OK,连接字符串为:

  

服务器= 127.0.0.1;数据库=;密码=;

我想这是完全符合逻辑的,但我需要测试是否建立了与数据库的连接,如果我用任意随机值填充登录文本框,则建立连接。

问题:我如何使用它来测试是否与数据库建立了连接?

2 个答案:

答案 0 :(得分:3)

连接到数据库,发出简单的查询,例如SELECT 1 from <KnownTable>,并确保您成功收到&#39; 1&#39;背部。这证实你有

  1. 连接到物理服务器(无网络问题)
  2. 使用数据库服务器进行身份验证(数据库正在运行且您具有正确的凭据)
  3. 连接到正确的数据库

答案 1 :(得分:1)

试试这个:

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    ApplicationSettings applicationSettings = new ApplicationSettings();
    applicationSettings.ServerIp = tbServer.Text;
    applicationSettings.MakeConnectionString();
   try
    {
         MySqlConnection connection = new MySqlConnection(applicationSettings.ConnectionString);
        connection.Open();
        MessageBox.Show(this, "server found", "OK !", MessageBoxButton.OK, MessageBoxImage.Information);
        connection.Close();
        applicationSettings.ServerDatabase = tbdbName.Text;
            try{
                applicationSettings.MakeConnectionString();
                connection = new MySqlConnection(applicationSettings.ConnectionString);
                connection.Open();
                MessageBox.Show(this, "Database found", "OK !", MessageBoxButton.OK, MessageBoxImage.Information);
                connection.Close();
                applicationSettings.ServerUserName = tbUsername.Text;
                applicationSettings.ServerPassword = pbPassword.SecurePassword;
                        try{
                            applicationSettings.MakeConnectionString();
                            connection = new MySqlConnection(applicationSettings.ConnectionString);
                            connection.Open();
                            MessageBox.Show(this, "Server,database and account are valid", "OK !", MessageBoxButton.OK, MessageBoxImage.Information);
                            }
                        catch(Exception ee){
                                           MessageBox.Show(this, "Error: Account  parameters not valid!! " + ee.Message, "connection ERROOOOR", MessageBoxButton.OK,
                                           MessageBoxImage.Error);
                                           }
                           }
             catch(Exception ee){
                            MessageBox.Show(this, "Error: server is connected but the database not found!! " + ee.Message, "connection ERROOOOR", MessageBoxButton.OK,
                            MessageBoxImage.Error);
                                 }
    }
    catch (Exception ee)
    {
        MessageBox.Show(this, "Error: server not found " + ee.Message, "connection ERROOOOR", MessageBoxButton.OK,
            MessageBoxImage.Error);

    }
    finally
    {
        if(connection.State == ConnectionState.Open)
            connection.Close();
    }

}

也许它可以帮到你;)