简体   繁体   English

如何让用户手动输入主键

[英]How to let the user to enter primary keys manually

I have a requirement for my ASP.NET website which requires me to let the user to enter primary keys manually for a particular table, and if the user entered a primary key which is already exist in the database, the system should notify the user as "Primary already exist" or some thing like that. 我对我的ASP.NET网站有一个要求,该要求要求我让用户手动输入特定表的主键,如果用户输入了数据库中已经存在的主键,则系统应通知用户“主要已经存在”或类似的东西。

I am thinking to let the user enter the primary key, and when he enter a key which is already exist, the system going to throw an exception for primary key constraint violation. 我正在考虑让用户输入主键,并且当他输入已经存在的键时,系统将抛出违反主键约束的异常。 So I'm going to catch that exception and in the catch block I am going to display an error message to notify the user about the duplication of the primary key. 因此,我将捕获该异常,并且在catch块中,我将显示一条错误消息,以通知用户有关主键重复的信息。 However, I am not sure this is a right way to do it or is there any standard way to do this? 但是,我不确定这是正确的方法还是有标准的方法?

Yes, this is a valid approach. 是的,这是一种有效的方法。 There are a few things that should be mentioned, though: 但是,有几件事需要提及:

  • Using a surrogate key (a counter, a GUID, ...) as the primary key instead of a user-entered value (natural key) has a lot of advantages, see the linked article for details. 使用代理键 (计数器,GUID等)作为主键而不是用户输入的值(自然键)有很多优点,有关详细信息,请参见链接的文章。 If you have already thought about that and decided to use a natural key instead, that's fine, I just thought it should be mentioned. 如果您已经考虑过这一点,并决定使用自然键,那很好,我认为应该提到它。 If you use a surrogate key, uniqueness of the user-defined value can be ensured through a unique index. 如果使用代理键,则可以通过唯一索引确保用户定义值的唯一性。

  • Throwing an exception as part of the normal control flow is usually frowned upon: Doing a SELECT for the existing key and then the INSERT would be more elegant than waiting for the exception to occur. 作为正常控制流的一部分,通常不赞成抛出异常:对现有键进行SELECT,然后INSERT会比等待异常发生更为优雅。 (Use a transaction and an appropriate transaction isolation level to ensure that no row can be inserted by another client in between your SELECT and your INSERT). (使用事务和适当的事务隔离级别,以确保在SELECT和INSERT之间没有其他客户端可以插入任何行)。

Before trying to insert the new record in the database, simply do a query against the target table to see if the primary key already exists. 在尝试将新记录插入数据库之前,只需对目标表进行查询以查看主键是否已经存在。 If it does, return a message to the user. 如果是这样,则向用户返回一条消息。

That's pretty ugly. 那太丑了。 It's not a good idea to use exceptions for "normal" logic flow - it makes the code harder to read and maintain, and tends to be slower (though you probably won't notice the performance impact). 在“常规”逻辑流中使用异常不是一个好主意-它使代码更难以阅读和维护,并且往往更慢(尽管您可能不会注意到性能影响)。

I'd split it up into a "validate" method, which should validate the data the user entered - is it the right data type? 我将其拆分为“验证”方法,该方法应验证用户输入的数据-正确的数据类型吗? Does it meet minimum length requirements? 是否符合最小长度要求? Is it unique? 它独特吗?

If the "validate" method returns a violation, show it to the user; 如果“验证”方法返回违规,则将其显示给用户。 give them the chance to fix it. 给他们修复的机会。

If the "validate" method does not return a violation, try to insert the record, and catch the "duplicate key" exception (someone else may have entered that very record in the time between your validation and entering the record). 如果“验证”方法未返回违规,请尝试插入记录,并捕获“重复键”异常(在验证和输入记录之间,可能有人输入了该记录)。

I'm not gonna discuss if you should or shouldn't do it (I don't think you should by the way :p ) but this is the code: 我不会讨论您是否应该这样做(我不认为您应该:p),但这是代码:

SET IDENTITY_INSERT TABLE ON

remember that you can have only one table per database set to ON 请记住,每个数据库只能将一个表设置为ON

The primary key in MS Sql Server is an unique index. MS Sql Server中的主键是唯一索引。 In case when you violate the index constraint, database will raise an error and rollback the transaction. 如果违反索引约束,数据库将引发错误并回滚事务。

If you will be able to maintain the indexes name properly, you will be able to use proposed by you solution for other cases to. 如果您能够正确维护索引名称,则可以将您的解决方案提议用于其他情况。

IMHO, that approach is fine. 恕我直言,这种方法很好。 Often alternative is to ask about the existence of values in database. 通常,替代方法是询问数据库中值的存在。 That eventually also end up with an Exception if was found. 如果找到了,那最终也将导致异常。 If you will decide to let the db to raise the exception do not forget to mention that in documentation of code. 如果您决定让数据库引发异常,请不要忘记在代码文档中提及它。 Because the usage of it without such information could cause in future some problems. 因为在没有此类信息的情况下使用它可能会在将来引起一些问题。

Why bother the user with making up a primary key and guessing what to enter and whether it's already taken? 为什么要打扰用户以构成主键并猜测要输入的内容以及是否已被使用呢? I mean, I know int is a wide range of numbers, but users will probably be trying 3-4 digit numbers; 我的意思是,我知道int是各种各样的数字,但是用户可能会尝试使用3-4位数字; eventually, they'll start hitting dups. 最终,他们将开始击球。 So, why?? 所以为什么?? You're complicating unnecessarily. 您不必要地使工作复杂化。

Have the db generate the id. 让数据库生成ID。 Report it to the user, if necessary. 如有必要,向用户报告。 Can't be simpler. 再简单不过了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何让用户输入整数并基于该值启动计时器 - How to let user enter an integer and start timer based off on that value 如何让用户从控制台输入任意数量的变量 - How to let the user enter any amount of variable from Console 手动为主键创建索引是一种好习惯吗? - Is it a good practice to manually create indexes for the primary keys? 如何获得主键 - How to get primary keys 如何确保用户不会尝试输入已经存在的主键ID? - How can i ensure a user does not attempt to enter an already existing primary key ID? 不允许用户在组合框(而不是数据绑定列表)中输入值 - not to let user to enter value in combobox other than databound list 当用户在带有 AcceptsReturn = "True" 的 UWP 文本框中按 CTRL+ENTER 键时如何避免换行 - How to avoid newline when user presses CTRL+ENTER keys in a UWP TextBox with AcceptsReturn = "True" C#我如何要求用户在输入无效输入时按任意键退出 - c# how do i ask the user to press any keys to exit when they enter an invalid input 如果元素位于页面的用户控件中,如何访问元素并输入键? - How do I access an element and enter keys if the element is in a user control in a page? 强制用户在使用WIA时手动输入凭据 - Force user to enter credentials manually while using WIA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM