[英]Update SQL query, unsure why it isn't working as no errors are appearing
我已经盯着这个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)
{
}
}
}
我认为问题在于您正在异步执行该命令(BeginExecuteNonQuery),但从未调用EndExecuteNonQuery来提交它。 我还怀疑您可以像这样同步调用它:
command20.ExecuteNonQuery();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.