2

这是问题所在。我正在尝试执行查询及其抛出和异常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");
    }
4

2 回答 2

1

我将建议您执行以下操作:

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 模式以便每次都将其处理掉,并始终从您的连接开始。

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

于 2013-02-27T22:58:20.193 回答
0

正如迈克所说,你总是最好使用“使用”块,因为一旦它退出使用块,它就会处理任何连接。我使用了两个 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");
          }
        }
    }
于 2013-02-27T23:05:09.827 回答