如何检查用户ID的可用性 [英] How do I check an availability of User Id

查看:83
本文介绍了如何检查用户ID的可用性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户为注册创建用户ID时,应该从数据库检查用户ID是否可用,如果有用,则用户应获得一些可用性用户ID ..

示例 -

用户ID- bapunmca

有人已经拥有该用户名。试试另一个?

可用:bapunmca52,as7562733



先谢谢

解决方案

< blockquote>执行SQL SELECT命令:

  SELECT  COUNT(*) FROM  MyTable  WHERE  [用户 ID] = '  bapunmca' 

如果它返回零,则id未被使用。



如果从C#执行此操作,则使用ExecuteScalar方法,它将直接返回值而无需读取器或DataAdapter:但请确保使用参数化查询或冒SQL注入风险这可能会破坏或破坏你的数据库。


[我假设SQL作为后端]



首先使用以下方法检查记录是否存在: http://stackoverflow.com/a/17 249135 [ ^ ]



如果存在,请根据您的要求生成建议。您可以参考以下链接: http://www.sqlteam.com/article/ custom-auto-generated-sequences-with-sql-server [ ^ ]如果要显示从数据库生成的建议或在C#处理它。



问候。 。


我根本不会从代码中进行检查。一个问题是,当您进行检查并发现数据库中不存在该ID然后添加它时,另一个会话可能会同时执行完全相同的操作,并且两个会话都认为该ID不存在。这将导致重复的ID。



因此我会让数据库负责检查。我将ID定义为表中的唯一列,当尝试插入重复时,数据库会自动生成错误。您唯一需要做的就是对重复错误实施错误处理



有关详细信息,请参阅创建唯一约束 [ ^ ]


When User create a User Id For Registration, it should check from Database whether the User Id Available or not, if it is availed then user should get some availability user id..
Example-
User Id- bapunmca
Someone already has that username. Try another?
Available: bapunmca52, as7562733

Thanks In Advance

解决方案

Execute an SQL SELECT command:

SELECT COUNT(*) FROM MyTable WHERE [User ID] = 'bapunmca'

If it returns zero, then the id is not in use.

If you execute this from C#, then use the ExecuteScalar method and it will return the value directly without a reader or DataAdapter being needed: but make sure you use a parameterised query or you risk SQL Injection which could damage or destroy your database.


[I assume SQL as backend]

First check if record exists using this : http://stackoverflow.com/a/17249135[^]

If it exists, generate suggestions based on your requirement. You can refer this link : http://www.sqlteam.com/article/custom-auto-generated-sequences-with-sql-server[^] if you want to show suggestion generated from database or handle it at C#.

Regards..


I wouldn't make the check from the code at all. One problem is that when you make the check and discover that the ID does not exist in the database and then add it, another session may do the exact same at the same time and both sessions believe that the ID does not exist. This would result to a duplicate ID.

So instead I'd let the database to take care of the check. I would define the ID as unique column in the table and when a duplicate is trying to be inserted an error is generated automatically by the database. The only thing you would need to do is to implement error handling for duplicate errors

For more information see Create Unique Constraints[^]


这篇关于如何检查用户ID的可用性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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