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

マイクが言ったように、ブロックを使用しなくなると接続が破棄されるため、常に「使用中」ブロックを使用することをお勧めします。接続用とコマンド オブジェクト用に 1 つの下に 2 つの 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 に答える