如何创建唯一的用户名 [英] How to create a Unique UserName

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

问题描述

尊敬的专家!正在使用Asp.net,c#,sqlserver 2005

在我的项目中,我有一个注册表.而且我在数据库中有注册表.
所以我需要创建一个唯一的用户名.

假设用户使用一个名称注册,而其他用户不应该使用该名称创建.

因此,我需要使用此名称的消息,例如Already Exists,请尝试使用其他名称.


请给我建议,并帮助我,该怎么做?

Dear Experts !! am working on Asp.net,c#,sqlserver 2005

In my project, i have a Registration Form. and also i have Registration Table in database.
So i need to create a Usernames as Unique.

Suppose if the User Registered with one name and the other users should not create with this name.

So , i need a Message like Already Exists with this Name, Try another name.


Please Suggest me, and help me, How to do this ?

Thanks.

推荐答案

您可以使用TexboxChanged事件并使文本框属性Autopostback为True.

现在在文本框中已更改事件:


You can use TexboxChanged Event and make textbox property Autopostback True.

now in Textbox changed Event :


 string pass="select username from tablename where username='"+txt_username.Text+"'";
SqlDataAdapter ad = new SqlDataAdapter(pass,connection object);
DataSet ds = new DataSet();
ad.Fill(ds);
if (ds.Tables[0].Rows.Count != 0)
{
            //Means Already Exits....
         txt_username.Focus();
//Display Message like Already Exits...
}
else
{
           // Means New USer
}




Enjoyyyy
Mitesh




Enjoyyyy
Mitesh





您可以简单地在存储过程中执行此操作

试试这个

hey


you can do this in your stored procedure simply

try this

as
begin
if exists(select user_Name from Tbl_name where UserName= @userName )
begin
select 0
end
else
begin
-- Here write your insert query ---
end 
begin
select 1
end




编写完此代码后,在文本框附近贴一个标签,然后显示您的消息.
希望这会对您有所帮助.




after writing this take a lable near your text box and show your message.
Hope this will help you.


在我看来,处理此问题的最佳方法是在数据库中将用户名定义为唯一.如果用户名已经存在,则数据库将以这种方式生成并出错(然后您可以作为异常捕获).

请参阅: SQL唯一约束 [唯一约束 [ ^ ]
In my opinion the best way to handle this is to define the user name as unique in the database. This way the database would generate and error (which you can then catch as an exception) if the username already exists.

See: SQL UNIQUE Constraint[^] and UNIQUE Constraints[^]


这篇关于如何创建唯一的用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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