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

本文关键字:检查 mysql 数据库连接 net 应用程序 | 更新日期: 2023-09-27 18:16:04

首先我想说的是,我一直在网上寻找解决我问题的方法,但我找到的都是经典的

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"。并单击测试连接按钮,显示消息框OK,连接字符串为:

=

Server = 127.0.0.1;数据库;密码=,

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

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

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

连接到数据库,发出一个简单的查询,如SELECT 1 from <KnownTable>,并确保成功地接收回'1'。这确认您有

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

try this:

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();
    }
}

也许对你有帮助;)