如何在Sql Server中停止重复插入。因为主键是Autoincreament [英] How Do I Stop Duplicate Insertion In Sql Server .As Primary Key Is Autoincreament

查看:112
本文介绍了如何在Sql Server中停止重复插入。因为主键是Autoincreament的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的asp.net应用程序在实时服务器上运行时。当服务器响应慢时,用户可以在成功收到消息之前多次点击保存按钮。



如果用户在按钮上单击两次,则在同一时间插入两个相同的记录,然后在成功消息和所有字段后为空。我如何解决此问题?

Sqlserver中的注意主键ID是自动增量集。

When My asp.net Application is running on live Server.when Server is responding Slow , User Can click on Save button Multiple time before Successfull Message receive.

If user clicking twice time on button then Two same Record Inserted at one time and then after successful message and all fields blank.How could I resolve this Issue ?
Note in Sqlserver Primary Key ID is Auto Increment Set.

推荐答案

我会在第一次点击后禁用提交按钮...
I would disable the submit button after the first click...


你可以检查记录是否存在或不是在插入之前。



此时你有两种选择。

1-如果存在另一个具有相同字段的记录,您可以完全禁止插入。

2-如果实际上有这样的重复数据,您可以设置一个时间限制可能存在。此选项需要在桌面上添加DateTime insertDate列。



为第一个选项,

you can check whether a record exists or not before inserting it.

you have two options at this point.
1- you may ban an insertion completely if there exists another record having the same fields.
2- you may put a time-limit for it if actually such duplicated data may exist. this option requires additional 'DateTime insertDate' column on your table.

for the first option,
if not exists select 1 from TableA where ColA=@a and ColB=@b
insert into TableA(ColA, ColB) values(@a,@b)





第二个选项



for the second option

if not exists select 1 from TableA where ColA=@a and ColB=@b and DATEDIFF(mi,insertDate, GETDATE())<5 
insert into TableA(ColA, ColB, insertDate) values(@a,@b,GETDATE())


成功提交后,您可以清除所有字段并在提交中按钮代码你可能已经对空白字段使用了正确的验证..



否则你可以在sql查询中使用检查重复字段。
After submitting successfully you can clear all fields & in submit button code you may have used proper validations for blank fields..

Other wise you can use check for duplicate fields in sql query .


这篇关于如何在Sql Server中停止重复插入。因为主键是Autoincreament的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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