如何使用c#防止Access数据库中的重复条目? [英] How to prevent duplicate entries in an Access database using c#?

查看:344
本文介绍了如何使用c#防止Access数据库中的重复条目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张MS Access表,其中包含一列和许多行.保存到此表的文本框的值.我想防止重复的条目被提交.例如,如果我在文本框中键入"ankush",并且该条目已经存在于表中,那么我想显示一个msgbox,表明该条目已存在于表中.使用c#

解决方案

如果该字段不是您的主键字段,则可以在包含"ankush"的字段上设置索引,并使该索引唯一

[Index=Yes(no duplicates)]

然后,Jet DB Engine将不允许插入,并显示默认消息.

我正在使用Access的旧版本,因此您的里程可能会有所不同,但是要显示自定义错误消息,您必须首先对表中的值进行查询,如下所示:

SELECT COUNT(MyField)as violated FROM MyTable WHERE MyField = 'input value here'

如果违反了代码,则在代码中分支> 0以显示您的消息框.

i have a table of MS Access which contains one column and many rows. The value of a textbox saved to this table. I want to prevent duplicate entries from being submitted. For example, if I type "ankush" in the textbox and this entry already exists in table, then I want to display a msgbox saying that this already exists in the table. using c#

解决方案

If the field is not your primary key field you can set an index on the field that contains 'ankush' and make that index unique

[Index=Yes(no duplicates)]

then the Jet DB Engine will not allow the insert and will show a default message.

I am working with an older version of Access, so your mileage may vary, but to show a custom error message you would have to first do a query for the value in the table like this:

SELECT COUNT(MyField)as violated FROM MyTable WHERE MyField = 'input value here'

Then branch in your code if violated > 0 to show your message box.

这篇关于如何使用c#防止Access数据库中的重复条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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