Sql数据库没有更新 [英] Sql database is not updating
本文介绍了Sql数据库没有更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图用两个复合主键更新一个表,但它不会更新。
我尝试过:
im trying to update a table with two composite primary keys but it wont update.
What I have tried:
public void update(MOduleQuestion moduleq)
{
string mcode = moduleq.getModuleCode();
int QNo = moduleq.getQuestionNo();
string Question = moduleq.getQuestion();
string OPt1 = moduleq.getOP1();
string OPt2 = moduleq.getOP2();
string OPt3 = moduleq.getOP3();
string OPt4 = moduleq.getOP4();
string Answer = moduleq.getAnswer();
try
{
//string sql = "Update Module_Question SET Question='" + Question + "',Option1='" + OPt1 + "',Option2='" + OPt2 + "',Option3='" + OPt3 + "',Option4='" + OPt4 + "' WHERE MQ_Module_Code = '" + mcode + "' AND MQ_Module_No= '" + QNo + "' ";
string sql = "Update Module_Question SET Question = @question, OPt1 = @opt1, OPt2 = @opt2, OPt3 = @opt3, OPt4 = @opt4, Answer = @answer WHERE MQ_Module_Code = @mcode and MQ_Question_No = @qno";
SqlCommand cmd = new SqlCommand(sql, m_con);
cmd.CommandType = System.Data.CommandType.Text;
cmd.Parameters.AddWithValue("@mcode", mcode);
cmd.Parameters.AddWithValue("@qno", QNo);
cmd.Parameters.AddWithValue("@question", Question);
cmd.Parameters.AddWithValue("@opt1", OPt1);
cmd.Parameters.AddWithValue("@opt2", OPt2);
cmd.Parameters.AddWithValue("@opt3", OPt3);
cmd.Parameters.AddWithValue("@opt4", OPt4);
cmd.Parameters.AddWithValue("@answer", Answer);
//SqlCommand cmd = new SqlCommand(sql, m_con);
m_con.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("Question Record Updated Successfully", "lecture Reg System", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (SqlException)
{
MessageBox.Show("Cannot Update", "lecture Reg System", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
m_con.Close(); // connection close
}
}
推荐答案
//Update a lecture
public void update(MOduleQuestion moduleq)
{
string mcode = moduleq.getModuleCode();
int QNo = moduleq.getQuestionNo();
string Question = moduleq.getQuestion();
string OPt1 = moduleq.getOP1();
string OPt2 = moduleq.getOP2();
string OPt3 = moduleq.getOP3();
string OPt4 = moduleq.getOP4();
string Answer = moduleq.getAnswer();
try
{
//string sql = "Update Module_Question SET Question='" + Question + "',Option1='" + OPt1 + "',Option2='" + OPt2 + "',Option3='" + OPt3 + "',Option4='" + OPt4 + "' WHERE MQ_Module_Code = '" + mcode + "' AND MQ_Module_No= '" + QNo + "' ";
string sql = "Update Module_Question SET Question = @question, Option1 = @opt1, Option2 = @opt2, Option3 = @opt3, Option4 = @opt4, Answer = @answer WHERE MQ_Module_Code = @mcode and MQ_Question_No = @qno";
SqlCommand cmd = new SqlCommand(sql, m_con);
cmd.CommandType = System.Data.CommandType.Text;
cmd.Parameters.AddWithValue("@mcode", mcode);
cmd.Parameters.AddWithValue("@qno", QNo);
cmd.Parameters.AddWithValue("@question", Question);
cmd.Parameters.AddWithValue("@opt1", OPt1);
cmd.Parameters.AddWithValue("@opt2", OPt2);
cmd.Parameters.AddWithValue("@opt3", OPt3);
cmd.Parameters.AddWithValue("@opt4", OPt4);
cmd.Parameters.AddWithValue("@answer", Answer);
//SqlCommand cmd = new SqlCommand(sql, m_con);
m_con.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("Question Record Updated Successfully", "lecture Reg System", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
finally
{
m_con.Close(); // connection close
}
}
这篇关于Sql数据库没有更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文