簡體   English   中英

無法連接到任何指定的MySQL主機

[英]Unable to connect to any of the specified MySQL hosts

這是問題所在。 我正在嘗試在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");
    }

我將建議您執行以下操作:

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對象替換我的連接!

正如Mike所說,您總是最好使用“ using”塊,因為一旦它退出了using塊,它就會處置任何連接。 我在下面使用了兩個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");
          }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM