如何在SQL更新表? [英] How to update table in SQL?
本文介绍了如何在SQL更新表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试更新在SQL数据库中的表Student_Course,它运行,但我不断收到
主键违反,无法进入复制
块引用>
我真的只是需要更新标记值,但需要将其匹配的CID(课程编号)和SID(学生证)
私人无效btnAdd_Click(对象发件人,EventArgs五)
{
使用(SqlConnection的CON =新的SqlConnection(CS))
{
试
{
使用(VAR CMD =新的SqlCommand(UPDATE Student_Course SET CID = @的CID,SID = @ SID,马克= @马克CON))
{
cmd.Connection = CON;
con.Open();
cmd.Parameters.AddWithValue(@ CID,cboCID.GetItemText(cboCID.SelectedItem));
cmd.Parameters.AddWithValue(@ SID,cboSID.GetItemText(cboSID.SelectedItem));
cmd.Parameters.AddWithValue(@标志,Convert.ToInt32(txtMark.Text));
如果(cmd.ExecuteNonQuery()0)
{
MessageBox.Show(标记添加了);
}
}
}
赶上(异常前)
{
MessageBox.Show(插入过程中的错误:+ ex.Message);
}
}
}
解决方案UPDATE Student_Course SET马克= @马克WHERE CID = @的CID和SID = @ SID
这应该是正确的SQL语句,因为你要更新你的
Student_Course <在
标记
字段/ code>根据CID表
&安培;SID
的人。Trying to update the table Student_Course in a database in SQL, it runs but I keep getting
Violation of Primary Key, Cannot enter duplicate
I really just need to update the marks value but need to match it to the CID (Course ID) and SID(Student ID)
private void btnAdd_Click(object sender, EventArgs e) { using (SqlConnection con = new SqlConnection(cs)) { try { using (var cmd = new SqlCommand("UPDATE Student_Course SET CID=@CID, SID=@SID , Mark=@Mark", con)) { cmd.Connection = con; con.Open(); cmd.Parameters.AddWithValue("@CID", cboCID.GetItemText(cboCID.SelectedItem)); cmd.Parameters.AddWithValue("@SID", cboSID.GetItemText(cboSID.SelectedItem)); cmd.Parameters.AddWithValue("@Mark", Convert.ToInt32(txtMark.Text)); if (cmd.ExecuteNonQuery() > 0) { MessageBox.Show("Mark Added"); } } } catch (Exception ex) { MessageBox.Show("Error during insert: " + ex.Message); } } }
解决方案"UPDATE Student_Course SET Mark = @Mark WHERE CID=@CID AND SID=@SID"
This should be the correct SQL statement because you are trying to update the
Mark
field in yourStudent_Course
table based on theCID
&SID
of the person.这篇关于如何在SQL更新表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文