保存前C#重复输入 [英] C# duplicate entry before saving

查看:94
本文介绍了保存前C#重复输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

海,

我将要开发仅停留在启动阶段的程序

问题:当用户输入一些文本并单击保存时,应与数据库检查该文本是否相同,如果重复则说重复输入",否则保存到数据库中

ThnQ

Hai,

I am about to developing a program which is stucked at starting only

Problem : When a user enter some text and click save that should be checked with database whether it is same or not and say Duplicate entry if same, else save to the database

ThnQ

推荐答案

我认为您应该利用数据库的功能来增强数据的完整性.

最简单的方法可能是在表上定义唯一约束.此约束将确保表中不存在重复的行.如果尝试添加重复项,则会引发异常,然后可以在您的应用程序中捕获该异常.

要了解有关唯一约束的更多信息,请参见: http://msdn.microsoft. com/en-us/library/ms191166(v = sql.105).aspx [
In my opinion you should utilize the capabilities of the database to enforce the integrity of the data.

The easiest way is perhaps to define a unique constraint on the table. This constraint would ensure that no duplicate rows exist in the table. If a duplicate is trying to be added an exception is thrown which you can then catch in your application.

To learn more about unique constraint, see: http://msdn.microsoft.com/en-us/library/ms191166(v=sql.105).aspx[^]




您可以在存储过程中编写逻辑.

请检查本文:
插入,更新,搜索和删除(CRUD操作)使用ASP.Net和MySQL [ ^ ]

您尚未标记正在使用的数据库.无论如何,这是一些用于存储过程检查的代码.我让你安息,

Hi,

You can write your logic in Stored procedure.

Please check this article :Insert, Update, Search and Delete (CRUD operation) using ASP.Net and MySQL[^]

You have not tag which database you are using.Anyway Here is some code for stored procedure checking. i leave rest on you,

CREATE PROCEDURE `InsertCountry`(
IN     _CountryName                       varchar(200),
OUT    _ReturnValue                       int
)
BEGIN
 
DECLARE IsCountryExist int(5);
--check if country already exist in database. 
SELECT COUNT(NM_COUNTRY_ID) INTO IsCountryExist FROM
       TBL_COUNTRYMASTER WHERE VC_COUNTRY_NAME = _CountryName;
 
START TRANSACTION;
-- insert record only if duplicate country is not in database.
IF IsCountryExist = 0 THEN
   INSERT INTO TBL_COUNTRYMASTER(VC_COUNTRY_NAME)
          VALUES (_CountryName);
 
END IF;
 
SELECT LAST_INSERT_ID() INTO _ReturnValue;
 
COMMIT;
END   



希望你能从我的解决方案中得到答案.

谢谢
-Amit Gajjar



Hope you got answer from my solution.

Thanks
-Amit Gajjar


让我们尝试使用EmployeeDetails,而您正在尝试插入新的/现有的EmpID.

Lets try with EmployeeDetails, and you are trying to INSERT new/existing EmpID.

INSERT INTO dbo.EmployeeDetails(EmpID)
SELECT '123456'
WHERE NOT EXISTS (SELECT EmpID FROM dbo.EmployeeDetails WHERE EmpID = '123456')



仅当123456不存在时,此处123456才会进入该表.

谢谢,

Kuthuparakkal



Here 123456 will get into this table only if 123456 does not exist.

Thanks,

Kuthuparakkal


这篇关于保存前C#重复输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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