无法连接到任何指定的MySQL主机

本文关键字:MySQL 主机 任何指 连接 | 更新日期: 2023-09-27 18:01:33

问题在这里。我试图在connection.Open执行查询及其抛出和异常。奇怪的是,在同一个应用程序上,我正在执行"选择"查询,它工作得很好。但是当我执行"更新"查询时,它抛出无法连接到任何指定的MySQL主机错误。我永远被困在这上面了。谁能指出我哪里错了?

 private void button1_Click(object sender, EventArgs e)
    {
        if (radioButton1.Checked)
        {
            timerEnabled = 1;
        }
        connection.Open();
        //update the settings to the database table 
        MySqlCommand command = connection.CreateCommand();
        command.CommandText = "update Admin_Settings set Difficulty='" + comboBox3.Text + "'," + "NoOfQuestions='" + comboBox4.Text + "'," + "NoOfChoices='" + comboBox5.Text + "'," +
            "Subject='" + comboBox8.Text + "'," + "Timer='" + comboBox2.Text + "," + "TimerEnabled=" + timerEnabled + "," + "TimerType='" + comboBox1.Text + "'";

        command.ExecuteNonQuery();
        MessageBox.Show("Settings updated");
    }

无法连接到任何指定的MySQL主机

我建议你这样做:

private void button1_Click(object sender, EventArgs e)
        {
            using (System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(connString))
            {
                if (radioButton1.Checked)
                {
                    timerEnabled = 1;
                }
                connection.Open();
                //update the settings to the database table 
                MySqlCommand command = connection.CreateCommand();
                command.CommandText = "update Admin_Settings set Difficulty='" + comboBox3.Text + "'," + "NoOfQuestions='" + comboBox4.Text + "'," + "NoOfChoices='" + comboBox5.Text + "'," +
                    "Subject='" + comboBox8.Text + "'," + "Timer='" + comboBox2.Text + "," + "TimerEnabled=" + timerEnabled + "," + "TimerType='" + comboBox1.Text + "'";

                command.ExecuteNonQuery();
                MessageBox.Show("Settings updated");
            }
        }

我知道你在想,你应该保持你的连接,方便使用等等,但在我的经验,这是浪费精力。最终发生的是很多你不想要或不需要的麻烦。你最终没有意识到你在其他地方有一个打开的连接,你花了几个小时来排除你不应该的事情。打开连接,完成后关闭。

如果你想有一个单独的连接对象,这是可以的,但是使用using模式,这样每次都可以处理它,并且总是重新开始你的连接。

注意:用你的MySqlConnection对象替换我的连接!

正如Mike所说,你总是最好使用"using" block,因为一旦它离开了using block,它就会处理任何连接。我在下面使用了两个using块,一个用于连接,另一个用于命令对象。

试试这个

    private void button1_Click(object sender, EventArgs e)
    {
        using (SqlConnection connection = new SqlConnection(connString))
        {
            if (radioButton1.Checked)
            {
                timerEnabled = 1;
            }
           string queryString = "update Admin_Settings set Difficulty='" +      
            comboBox3.Text + "'," + "NoOfQuestions='" + comboBox4.Text + "'," + 
            "NoOfChoices='" + comboBox5.Text + "'," + "Subject='" + comboBox8.Text +  
           "'," + "Timer='" + comboBox2.Text + "," + "TimerEnabled=" + timerEnabled +  
              "," + "TimerType='" + comboBox1.Text + "'";
          using (SqlCommand command = new SqlCommand(queryString, connection))
          {
            //update the settings to the database table 

            command.Connection.Open();
            command.ExecuteNonQuery();
            command.Connection.Close();
            MessageBox.Show("Settings updated");
          }
        }
    }