主键违规(重复键值?)

时间:2014-04-26 06:11:57

标签: c# sql sql-server

我正在创建一个程序,用于从数据库中添加/编辑/删除产品。

其他一切都很有效,但每当我尝试添加新产品(使用' 1234'进行测试)时,我都会收到primary key violation错误。

  

违反PRIMARY KEY约束' PK_Products'。无法插入   对象' dbo.Products'中的重复键。重复的键值是   (1234)。声明已经终止。

此产品编号绝对不存在,但在我之后搜索时仍然不存在。

任何人都可以帮我弄清楚可能导致这种情况的原因吗?我无法确定问题所在。

这是我的AddProduct方法。

public static bool AddProduct(Product product)
{
    SqlConnection connect = MMABooksDB.GetConnection();
    string insert = "INSERT Products (ProductCode, Description, UnitPrice) VALUES (@code, @description, @price)";
    SqlCommand insertCmd = new SqlCommand(insert, connect);
    insertCmd.Parameters.AddWithValue("@code", product.Code);
    insertCmd.Parameters.AddWithValue("@description", product.Description);
    insertCmd.Parameters.AddWithValue("@price", product.Price);
         try
            {
                connect.Open();
                int counter = insertCmd.ExecuteNonQuery();
                string selectStatement =
                    "SELECT IDENT_CURRENT('Products') FROM Products";
                SqlCommand selectCommand = new SqlCommand(selectStatement, connect);
                 int ProductCode;
                object dbProductID = selectCommand.ExecuteScalar();
             if (dbProductID != null || dbProductID != DBNull.Value)
                    ProductCode = 0;
                else
                    ProductCode = Convert.ToInt32(dbProductID);
                if (counter>0)
                    return true;
                else
                    return false;
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                connect.Close();
            }
}

这是Product

    public class Product
    {
        public string code;
        public string description;
        public decimal price;

        public Product() { }

        public Product(string code, string description, decimal price)
        {
            this.Code = code;
            this.Description = description;
            this.Price = price;
        }

        public string Code
        {
            get
            {
                return code;
            }
            set
            {
                code = value;
            }
        }

        public string Description
        {
            get
            {
                return description;
            }
            set
            {
                description = value;
            }
        }

        public decimal Price
        {
            get
            {
                return price;
            }
            set
            {
                price = value;
            }
        }

        public string GetDisplayText()
        {
            return code + ", " + price.ToString("c") + ", " + description;
        }

        public string GetDisplayText(string sep)
        {
            return code + sep + price.ToString("c") + sep + description;
        }
    }
}

今晚你们已经帮我完成了这个项目,我非常感激。感谢您提供的任何见解。如果有任何进一步的信息有用,请告诉我。

2 个答案:

答案 0 :(得分:2)

我猜你忘了在某处做产品代码+1。你为什么不在PK上使用自动增量?这将随时工作。

答案 1 :(得分:0)

据我所知,主键的插入中没有值(通常称为id)。

我猜你没有在那个领域使用某种自动递增。

相关问题