void Login()
{
string strConn=ConfigurationSettings.AppSettings["conn"];
SqlConnection myConn=new SqlConnection(strConn);
string strSQL ="select * from tb_UserInfo where UserName='"+tbUserName.Text.Trim()+"and userPass='"+tbUserPass.Text.Trim()+"'";
SqlCommand myCmd=new SqlCommand(strSQL,myConn);
myConn.open();
SqlDataReader myRead=myConn.ExecuteReader();
int i=0;
while(myRead.Read())
{
i++;
Session["UserName"]=myRead.GetString(1);
}
myConn.Close();
if(i==0)
{
Response.Write("<script>alert('用户名或密码错误!');</script>");
Response.Redirect("Login.aspx");
}else{
Response.Write("LoginSuccess.aspx?parm1="+tbUserName.Text.Trim());
}
}
欢迎加入:http://www.itpob.cn/bbs
本文介绍了一个使用C#和SQL Server实现的简单用户登录验证过程。该过程通过读取配置文件中的连接字符串来建立数据库连接,并执行SQL查询以验证用户名和密码是否匹配。如果匹配,则将用户名保存到会话中;如果不匹配,则提示用户错误信息。
13万+

被折叠的 条评论
为什么被折叠?



