2013-02-27 60 views
2

這是問題所在。我試圖執行一個查詢,並在connection.Open處拋出異常。奇怪的是,在同一個應用程序中,我正在執行「選擇」查詢並且它工作正常。但是當我執行「更新」查詢時,它會拋出這個無法連接到任何指定的MySQL主機錯誤。一直堅持這一點。有人可以發現我出錯的地方嗎?無法連接到任何指定的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"); 
    } 
+0

你的問題需要更多的信息。當connection.Open();'執行?似乎是一個可能的原因。 – evanmcdonnal 2013-02-27 22:56:59

回答

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對象!

+0

感謝您的建議和你的幫助。我已經設法修復它。連接字符串,我忘了放置分號。愚蠢的錯誤。 ahh – user123 2013-02-27 23:55:43

+0

但贊成推薦 – user123 2013-02-27 23:56:26

+0

np,我很高興你能解決它。 – 2013-02-27 23:57:36

0

正如Mike所說,你總是最好使用「使用」塊,因爲一旦它脫離使用塊,它就會處理任何連接。我使用了兩個以下的塊用於連接,其他用於命令對象。

試試這個

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"); 
      } 
     } 
    } 
+0

這是真的,但問題實際上並不在這裏,這是他連接的地方。 – evanmcdonnal 2013-02-27 23:16:03

+0

@evanmcdonnal:我修改了上面的代碼。在我們執行查詢之前,我們需要明確地打開連接。如果這也行不通,他應該拿出錯誤的詳細信息 – Learner 2013-02-27 23:26:51

+0

感謝您的建議和你的幫助。我已經設法修復它。連接字符串,我忘了放置分號。愚蠢的錯誤。 ahh – user123 2013-02-27 23:55:22

相關問題