如何减少重复插入表值。 [英] How to reduce the duplication insert the table values.
本文介绍了如何减少重复插入表值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在进行在线购买项目。由于我在产品表中存储了产品名称。例如我的产品名称是Pen。但我希望此产品只在产品表上插入一次。如何减少重复。
请帮助我!!!
I am doing online purchasing project.As i am stored product name in my Product Table.Example i have Product Name is Pen.but i want this product insert only one time on the product table. how reduce the duplication.
Please help me !!!
推荐答案
参考
在表之前检查重复项 - 插入数据到sql-server [ ^ ] br />
在插入插入表之前,我怎样才能检查重复项[ ^ ]
Refer
check-for-duplicates-in-the-table-before-inserting-data-into-sql-server[^]
how-can-i-check-for-duplicates-before-inserting-into-a-table-when-inserting[^]
尝试给它一个产品ID,对于数据库中的每个产品,它必须是唯一的。
在插入新产品的代码中,按如下方式捕获异常
Try with giving it a Product ID, that must be unique to the each product in your database.
In you code while inserting new Product, catch the exception as follows
try
{
/*
Some code for inserting record
*/
}
catch(SqlException ex)
{
if (ex.Number == 2627)
{
MessageBox.Show("Productis already there!");
}
}
MS-SQL服务器的错误号2627是违反Unique-Key
约束。
-KR
Error Number 2627 for MS-SQL server is for the violation of Unique-Key
Constraint.
-KR
这篇关于如何减少重复插入表值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文