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

时间:2015-04-27 21:21:52

标签: c# sql button sql-update

我已经盯着这个UPDATE声明了很长一段时间,并且不确定为什么我的表没有改变。当我按下按钮时没有出现错误,但是我的表也没有更新,我已经检查过我的所有变量都有调试值,而且它们都有。

我很感激任何人都可以给我的帮助!

这是包含我需要帮助的声明的代码:

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)
    {



    }
}

}

1 个答案:

答案 0 :(得分:3)

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

command20.ExecuteNonQuery();