您的位置:首页 > 编程语言 > ASP

Asp.Net登录

2014-03-31 22:59 393 查看
//在asp.net里头做个登录,用到ado.net方法和winform里头的一样简单

//拖三个控件,两个TextBox一个button就行了,注意了初学者不要拖成html的控件

界面:



为了养成习惯,我把连接字符串写在了web.config文件里头了



代码:

 

protected void Button1_Click(object sender, EventArgs e)
{
string id = TextBox1.Text.Trim();
string pwd = TextBox2.Text.Trim();
using (SqlConnection conn=new SqlConnection(connStr))
{
conn.Open();
using(SqlCommand cmd=conn.CreateCommand())
{
cmd.CommandText="select count(*) from login where id=@id and pwd=@pwd";
SqlParameter pm=new SqlParameter("@id",id);
SqlParameter pm2=new SqlParameter("@pwd",pwd);
cmd.Parameters.Add(pm);
cmd.Parameters.Add(pm2);
object obj=cmd.ExecuteScalar();
if (Convert.ToInt32(obj)>0)
{
Response.Write("<script>alert('登录成功!')</script>");
}
else
{
Response.Write("<script>alert('登录失败!')</script>");
}
}
}
}



 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  asp.net login ado.net