如何检查用户名可用性 [英] How to check username availability

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

问题描述



我是asp.net的新手。我正在设计一个新的登录表单,其中包含用户名和密码。我还有一个注册选项,如何编写我的代码,以便它可以检查天气是否可用的用户名。我正在使用SQL服务器和C#。

解决方案

你可以使用sql存储过程。



< pre lang =sql> 创建 proc_UserNameExist
@ UserName as Varchar 50
as
声明 @ Count int

select @ Count = Count(*) from tblUser 其中 userName = @ UserName
if (@ Count> 0)
开始
- - 用户找到了
end
else
开始
- - 找不到用户
end


web.Config //连接数据库

 <   connectionStrings  >  
< add 名称 = dbconnection connectionString = 数据源= SureshDasari;集成安全性= true;初始目录= MySampleDB / >
< / connectionStrings >







//行动你的按钮

 protected void btnSubmit_Click(object sender,EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings [dbconnection]。ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand(select * from UserInformation其中UserName = @ username和Password = @ password,con);
cmd.Parameters.AddWithValue(@ username,txtUserName.Text);
cmd.Parameters.AddWithValue(@ password,txtPWD.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if(dt.Rows.Count> 0)
{
Response.Redirect(Details.aspx);
}
else
{
ClientScript.RegisterStartupScript(Page.GetType(),validation,< script language ='javascript'> alert('无效的用户名和密码')< /脚本>中);
}
}





如果使用Sql Server身份验证,则添加UserId =和Password = 在Web.Config


我希望你在寻找这个:



http://www.aspdotnet-suresh.com/2011/03/how-to-check-username-availability。 html [ ^ ]

Hi,
I'm new to asp.net. I'm designing a new login form that takes username and password.I also have a register option and how can I write my code so that It checks weather the username available or not. I'm using SQL server and C#.

解决方案

You Can use sql Stored Proc.

Create proc_UserNameExist
@UserName as Varchar(50)
as
declare @Count int

select @Count=Count(*) from tblUser where userName=@UserName
if(@Count>0)
begin
---User Found
end
else
begin
---User Not Found
end


web.Config // Connect to database

<connectionStrings>
<add name="dbconnection" connectionString="Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB"/>
</connectionStrings>




//Action Your Button

protected void btnSubmit_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("select * from UserInformation where UserName =@username and Password=@password",con);
cmd.Parameters.AddWithValue("@username", txtUserName.Text);
cmd.Parameters.AddWithValue("@password", txtPWD.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if(dt.Rows.Count>0)
{
Response.Redirect("Details.aspx");
}
else
{
ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>");
}
}



If you using Sql Server Authentication, you add UserId="" and Password="" in Web.Config


i hope you are looking for this :

http://www.aspdotnet-suresh.com/2011/03/how-to-check-username-availability.html[^]


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

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