违反PRIMARY KEY约束'PK_Auto_Part_Details'。无法在对象'dbo.Auto_Part_Details'中插入重复键。重复键值为(1)。 [英] Violation of PRIMARY KEY constraint 'PK_Auto_Part_Details'. Cannot insert duplicate key in object 'dbo.Auto_Part_Details'. The duplicate key value is (1).
本文介绍了违反PRIMARY KEY约束'PK_Auto_Part_Details'。无法在对象'dbo.Auto_Part_Details'中插入重复键。重复键值为(1)。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
private void btn_Add_Click(object sender, EventArgs e)
{
try
{
for (int i = 0; i < dgv_Add_job_card.Rows.Count ; i++)
{
con.Open();
s = "insert into Auto_Part_Details (Items_No, Job_Card, Value_Part, Auto_Parts_id) values (@Items_No, @Job_Card, @Value_Part,@Auto_Parts_id )";
sd=new SqlCommand(s,con);
sd.Parameters.AddWithValue("@Items_No", dgv_Add_job_card.Rows[i].Cells[0].Value);
sd.Parameters.AddWithValue("@Job_Card", dgv_Add_job_card.Rows[i].Cells[1].Value);
sd.Parameters.AddWithValue("@Value_Part", dgv_Add_job_card.Rows[i].Cells[2].Value);
sd.Parameters.AddWithValue("@Auto_Parts_id",dgv_Add_job_card.Rows[i].Cells[3].Value);
sd.ExecuteNonQuery();
con.Close();
}
}
推荐答案
错误清楚地告诉您正在向<$ c插入值$ c> Id 这是主键并且该值已经存在。
这是因为主键是唯一的,不能重复。因此,请尝试插入唯一值。
The error clearly tells you that you are inserting a value to theId
which is a Primary Key and that value already exists.
This is because Primary Key is unique and can't be duplicated. So, try inserting a unique value.
这篇关于违反PRIMARY KEY约束'PK_Auto_Part_Details'。无法在对象'dbo.Auto_Part_Details'中插入重复键。重复键值为(1)。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文