2013-02-27 2 views
2

여기에 문제가 있습니다. connection.Open에서 쿼리 및 그 throw 및 예외를 실행하려고합니다. 이상하게도, 같은 애플리케이션에서 "선택"쿼리를 실행 중이며 정상적으로 작동합니다. 하지만 "업데이트"쿼리를 실행하면이 Unable이 지정된 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

답변

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

난 당신이 사용 저쩌구의 용이성에 대한 연결을 유지해야한다고, 자신에게 생각 만에 있는지 이해 내 경험, 그건 쓸데없는 노력이야. 당신이 원하지 않거나 필요로하지 않는 수많은 문제가 끝납니다. 결국 다른 곳에서 연결 고리가 열려 있다는 것을 깨닫지 못하고 결국은 안되는 문제를 해결하는 데 몇 시간을 소비하게됩니다. 연결이 완료되면 닫으십시오.

연결 개체를 하나만 갖고 싶다면 사용하는 패턴을 사용할 때마다 매번 처리되어야하며 항상 연결로 시작해야합니다.

참고 : 내 연결을 yoru MySqlConnection 개체로 바꾸십시오!

+0

조언과 도움을 주셔서 감사합니다. 나는 그것을 고칠 수 있었다. 연결 문자열, 세미콜론을 넣는 것을 잊어 버렸습니다. 멍청한 실수. ahh – user123

+0

이지만 권장 사항 – user123

+0

np를 추천합니다. 문제가 해결되어 기쁩니다. –

0

마이크가 말했듯이 블록을 사용하지 않으면 연결을 폐기하기 때문에 항상 "사용"블록을 더 잘 사용합니다. 저는 두 개의 블록을 연결 용으로 사용하고 다른 하나는 명령 객체 용으로 사용했습니다.

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

+0

@evanmcdonnal : 위 코드를 수정했습니다. 우리는 executenonquery를하기 전에 명시 적으로 연결을 열어야합니다. 이것도 작동하지 않으면 오류 세부 사항을 추가로 제시해야합니다. – Learner

+0

조언과 도움을 주셔서 감사합니다. 나는 그것을 고칠 수 있었다. 연결 문자열, 세미콜론을 넣는 것을 잊어 버렸습니다. 멍청한 실수. ahh – user123

관련 문제