1、数据库连接语句
<connectionStrings> <add name="yhotel" connectionString="Database=yhotel;Server=.;Integrated Security=false;Uid=sa;PassWord=zts;" providerName="System.Data.SqlClient" /> </connectionStrings>
其中 name="yhotel" 尽量和数据库同名,而且系统是通过这个节点名称获取连接字符串,是通过ConfigurationManager方法获取,一般写在 web.config文件中
2、点击清除按钮,清除用户名和密码
/// <summary> /// 清除用户名密码 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnClear_Click(object sender, EventArgs e) { txtUserName.Text = ""; txtPwd.Text = ""; }
3、点击登陆按钮 获取用户名和密码,利用sql语句到数据库中查询,返回一个userid ,判断,如果userid不为null,则认证成功,如果为Null,则不通过
/// <summary> /// 登陆按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnLogin_Click(object sender, EventArgs e) { string username = txtUserName.Text.Trim(); string pwd = txtPwd.Text.Trim(); if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(pwd)) { Response.Write("<script>alert(‘用户名或者密码不能为空‘);</script>"); } else { string strCon = ConfigurationManager.ConnectionStrings["yhotel"].ToString();//like成电话号码 using( SqlConnection con = new SqlConnection(strCon)) //like 电话 { con.Open();//和数据库建立起了连接 string strSql = string.Format("select USERID from UserList where UserName=‘{0}‘and PWD=‘{1}‘",username,pwd); SqlCommand cmd = new SqlCommand(strSql, con); using (SqlDataReader read = cmd.ExecuteReader()) { if (read.Read()) { Response.Write("<script>alert(‘登陆成功‘);</script>"); } else { Response.Write("<script>alert(‘用户名或密码错误‘);</script>"); } } } } }
时间: 2024-10-05 22:21:42