当我单击更新按钮时,它显示成功,但我的数据库未更新。 什么是错我的代码:为什么我的更新查询不起作用
private void CmdUpdate_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(@"Data Source......");
conn.Open();
SqlCommand comm = new SqlCommand("update Leaves_Type set Leaves='"+txtltype.Text+"' where Leaves='"+txtltype.Text+"'");
comm.Connection = conn;
comm.ExecuteNonQuery();
MessageBox.Show("Successfully Updated");
conn.Close();
}
你试过运行此确切的查询,看看它是否工作不使用c#? –
事实上,你没有使用查询参数,并且在表单方法中有数据库逻辑,它直接读取控件,这使我不得不脱发。 – Euphoric
@Euphoric。 SqlCommand通讯=新的SqlCommand(“更新Leaves_Type设置叶= @叶”); comm.Parameters.AddWithValue(“@ Leaves”,txtltype.Text);但它显示PRIMARY KEY约束'PK_Leaves_Type'的错误违规。无法在对象'dbo.Leaves_Type'中插入重复键。 该声明已被终止。 – user3488317