Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在.NET应用程序中正确检查与mysql的数据库连接?_C#_Mysql_Database - Fatal编程技术网

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

C# 如何在.NET应用程序中正确检查与mysql的数据库连接?,c#,mysql,database,C#,Mysql,Database,首先,我想说的是,我一直在互联网上寻找解决我问题的方法,而我所找到的只是经典 if(connection.open) return true; 解决方案在我的情况下不适合我的需要 我正在写一份申请书。在运行之前,我需要检查与MySQL数据库的连接,如果连接未完成,将打开一个新窗口,提示用户进行连接设置用户名、服务器地址、密码。。。。在此窗口中,我有一个测试连接按钮,以查看是否建立了连接,这是按钮单击事件的代码: private void Button_Click_1(object sende

首先,我想说的是,我一直在互联网上寻找解决我问题的方法,而我所找到的只是经典

if(connection.open) return true; 
解决方案在我的情况下不适合我的需要

我正在写一份申请书。在运行之前,我需要检查与MySQL数据库的连接,如果连接未完成,将打开一个新窗口,提示用户进行连接设置用户名、服务器地址、密码。。。。在此窗口中,我有一个测试连接按钮,以查看是否建立了连接,这是按钮单击事件的代码:

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

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

我想这是完全合乎逻辑的,但是我需要测试是否建立了到数据库的连接,如果我用任意值填充登录文本框,那么连接就建立了


问题:如何使用此选项测试是否与数据库建立了连接?

连接到数据库,发出一个简单的查询,如SELECT 1 from,并确保成功接收回“1”。这证实你有

连接到物理服务器没有网络问题 使用数据库服务器进行身份验证数据库正在运行,并且您具有正确的凭据 已连接到正确的数据库
连接到数据库,发出一个简单的查询,如SELECT 1 from,并确保成功接收回“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();
    }

}
也许它对你有帮助

试试这个:

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

}

也许它对你有帮助

thnks@Babak,那有帮助thnks@Babak,那有帮助