且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我必须在此代码中纠正什么?

更新时间:2023-02-14 22:17:06

添加
command.Parameters.Clear();





private void button1_Click(object sender, EventArgs e)
        {
            command.Parameters.AddWithValue("@StudentID", textBox1.Text);
            command.Parameters.AddWithValue("@Name", textBox2.Text);
            command.Parameters.AddWithValue("@Age", textBox3.Text);
            command.Parameters.AddWithValue("@Gender", textBox4.Text);
            command.Parameters.AddWithValue("@Courseno",comboBox1.Text);
            command.CommandText = "INSERT into details"+ "(StudentID,Name,Age,Gender,Courseno)VALUES" + "(@StudentID,@Name,@Age,@Gender,@Courseno)";

            try
            {
                con.Open();

                int result = command.ExecuteNonQuery();

                if (result > 0)
                    MessageBox.Show("student successfully updated");
                else
                    MessageBox.Show("failed to update");
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                con.Close();
                command.Parameters.Clear()
            }

            ClearFields();
        }