更新 SQL 查询,不确定为什么它不工作,因为没有出现错误

Update SQL query, unsure why it isn't working as no errors are appearing

我盯着这个 UPDATE 语句看了很长时间,不确定为什么我的 table 没有改变。当我按下按钮时,没有出现错误,但我的 table 也没有得到更新,我检查了我的所有变量在调试时都有值,它们确实有。

如果有人能给我任何帮助,我将不胜感激!

这是包含我需要帮助的语句的代码:

private void button1_Click(object sender, EventArgs e)
    {

        string studentanswertext = textBox1.Text;
        string connectionString = ConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString;
        string y = GlobalVariableClass.Signedinteacher;
        Convert.ToInt32(y);
        MessageBox.Show(y);

        MessageBox.Show(Convert.ToString(CurrentQuestionID));
        MessageBox.Show(studentanswertext);
        SqlConnection connect = new SqlConnection(connectionString);
        connect.Open();

        SqlCommand command20 = new SqlCommand(@"UPDATE QuestionStudentAssociation SET ([StudentAnswer]=@StudentAnswertext) WHERE ([QuestionID]=@CurrentQID AND [StudentID]=@SignedinStudent )", connect);
        command20.Parameters.AddWithValue("@StudentAnswertext", studentanswertext);
        command20.Parameters.AddWithValue("@CurrentQID", CurrentQuestionID);
        command20.Parameters.AddWithValue("@SignedinStudent", y);
        command20.BeginExecuteNonQuery();


        connect.Close();
    }

如果有人想查看它以防影响按钮甚至处理程序,这是我表单的完整代码:

 namespace ComputingA2_Official_Project
{
public partial class CurrentlySetTestForm : Form

{
    Timer loopTimer = new Timer();
    private int CurrentQuestionID { get; set; }
    private string QuestionSpace { get; set; }
    public CurrentlySetTestForm()
    {
        InitializeComponent();
    }

    private void CurrentlySetTestForm_Load(object sender, EventArgs e)
    {

        string y = GlobalVariableClass.Signedinteacher;

        Convert.ToInt32(y);

        string connectionString = ConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString;
        SqlConnection connect = new SqlConnection(connectionString);

        connect.Open();

        SqlCommand command18 = new SqlCommand("SELECT MIN([QuestionID]) AS QuestionID FROM QuestionStudentAssociation WHERE ( [StudentID]=@Signedinstudent AND [StudentAnswer] IS NULL )", connect);
        command18.Parameters.AddWithValue("@Signedinstudent", y);

        var reader = command18.ExecuteReader();
        while (reader.Read())
        {
            CurrentQuestionID = Convert.ToInt32(reader[0]);

            SqlCommand command19 = new SqlCommand("SELECT ([Question Space]) FROM Questions WHERE ([QuestionID]=@CurrentQID)", connect);
            command19.Parameters.AddWithValue("@CurrentQID", CurrentQuestionID);


            using (SqlDataReader reader2 = command19.ExecuteReader())
            {
                while (reader2.Read())
                {
                    QuestionSpace = Convert.ToString(reader2[0]);
                    label1.Text = QuestionSpace;
                }
            }
         }

        connect.Close();

    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {

        string studentanswertext = textBox1.Text;
        string connectionString = ConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString;
        string y = GlobalVariableClass.Signedinteacher;
        Convert.ToInt32(y);
        MessageBox.Show(y);

        MessageBox.Show(Convert.ToString(CurrentQuestionID));
        MessageBox.Show(studentanswertext);
        SqlConnection connect = new SqlConnection(connectionString);
        connect.Open();

        SqlCommand command20 = new SqlCommand(@"UPDATE QuestionStudentAssociation SET ([StudentAnswer]=@StudentAnswertext) WHERE ([QuestionID]=@CurrentQID AND [StudentID]=@SignedinStudent )", connect);
        command20.Parameters.AddWithValue("@StudentAnswertext", studentanswertext);
        command20.Parameters.AddWithValue("@CurrentQID", CurrentQuestionID);
        command20.Parameters.AddWithValue("@SignedinStudent", y);
        command20.BeginExecuteNonQuery();


        connect.Close();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {



    }
}

}

我认为问题在于您正在异步执行命令 (BeginExecuteNonQuery),但从未调用 EndExecuteNonQuery 来提交它。我还怀疑您可以像这样同步调用它:

command20.ExecuteNonQuery();