如何处理asp.net中的重复数据 [英] How to handle duplicate data in asp.net

查看:100
本文介绍了如何处理asp.net中的重复数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含4条记录的数据表,这4条记录也在数据库中,在这里我遇到这样的错误
< b>违反主键约束"PK_VMIS_30".无法在对象"VMIS_30"中插入重复的密钥.该语句已终止. </b>
我想显示一个消息框或在response.write("DATA ALREADY EXIXTS")语句中显示此问题的解决方案
在插入
之前,先谢谢

I have a datatable that contain 4 records, These 4 records r also in the database here i m getting an error like this
<b>Violation of PRIMARY KEY constraint ''PK_VMIS_30''. Cannot insert duplicate key in object ''VMIS_30''. The statement has been terminated. </b>
I want to show like an message box or in the response.write("DATA ALREADY EXIXTS") statement, Post solution for this
Thanks in advance

推荐答案

在数据库中 检查插入的数据是否存在
如果存在,则表明已存在该设置消息.

in database before inserting
check that the inserted data is exists or not
if it exists the set message that it already exists.

declare @message varchjar(50)
if exists(select id from table name)
{
set @ message="already exists";
}



并在后端获取此消息变量



and get this message variable at back end


其中之一:

1)首先检查.进行选择,如果记录返回,请报告问题
2)使用try-catch块并在那里报告错误.

我更喜欢前者,因为您可以更好地控制发生的事情.如果您依靠try catch,那么您将不知道在操作失败之前已添加了哪些记录(如果有).这意味着您下次也可能会收到该错误.就用户而言,这并不好-他们会让您感到沮丧和烦恼.

另外,通常将try-catch依赖于正常处理被认为是不良的编码样式.防御性编程要好得多.
Either:

1) Check first. Do a select, and if records come back, report the problem
2) Use a try-catch block and report the error there.

I would prefer the former, simply because you can control better what has happened. If you rely on the try catch, then you do not know which records have been added (if any) before the operation failed. This means that you are likely to get the error next time as well. Not good as far as the user is concerned - they will get frustrated and annoyed with you.

In addition, relying on try-catch for normal processing is generally considered a bad coding style. Defensive programming is much, much better.


这篇关于如何处理asp.net中的重复数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆