避免在C#中重复数据 [英] Avoid repeating data in c#

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

问题描述

大家好.

我有一个名为PlateSerial的文本框,我插入了唯一的序列号.这不是自动生成的.插入序列号后,它将被带入NumberPlate数据库.

我想要的是,如果我放置一个已经插入数据库的序列号.它不应该允许我这样做.

那我该怎么做.

请帮忙.另外,如果可能的话,我更喜欢代码示例.

谢谢大家

Hello everyone.

I have a textbox called PlateSerial were i insert unique serial numbers. This is not auto generated. Once the serial is inserted it is taken to the NumberPlate database.

What i want is that if i put a serial that has already been inserted in the database. It should not allow me to do so.

So how do i do this.

Please help. In addition if possible i would prefer code examples.

Thanks everyone

推荐答案

您可以通过两种方式进行.

1)建立逻辑以自己检查唯一值.在这种情况下,每次插入新行时,请先检查序列号是否已存在.在保存之前从数据库加载它们,然后对其进行检查将是最简单但不是最有效的方法.其他方法可能是缓存这些值等.

2)允许数据库为您执行此操作.在此序列号上设置UNIQUE 约束.柱子. [ ^ ]应该可以帮助您了解什么是唯一列. [ http://www.w3schools.com/sql/sql_primarykey.asp [ ^ ].
You can do this in two ways.

1) Build the logic to check for unique values yourself. In this case, everytime you insert a new row, first check if the serial number already exists. Loading them from the db before saving and then checking them would be the easiest though not the most efficient way. Other approaches could be to cache these values etc.

2) Allow the database to do this for you. Set a UNIQUE constrain on this serial no. column. This[^] should help you understand what a unique column is. This[^] should guide you futher and help you set up a constraint. Once this is setup, the database will not allow you to insert duplicate columns.

As a note, you should also look at primary keys - http://www.w3schools.com/sql/sql_primarykey.asp[^].


这篇关于避免在C#中重复数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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