using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Login : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } public bool checklogin(Sysuser user,ref string message) { string sqlconstr = "Data Source=.;Initial Catalog=Example;Integrated Security=true"; string sql = "select * from SystemUser where [email protected]"; SqlConnection con = new SqlConnection(sqlconstr); SqlCommand cmd = new SqlCommand(sql, con); SqlParameter[] para = { new SqlParameter("@userid",user.Userid), //new SqlParameter("@userpwd",user.Pwd), }; con.Open(); cmd.Parameters.AddRange(para); SqlDataReader sdr = cmd.ExecuteReader(); if(sdr.HasRows) { if(sdr.Read()&&sdr["userpwd"].ToString()==user.Pwd) { con.Close(); message = "成功"; return true; } else { message = "密码错误!"; return false; } } else { message = "没有此用户"; return false; } } protected void btnlogin_Click1(object sender, EventArgs e) { //获取用户输入的信息 Sysuser user = new Sysuser(); user.Userid = txtuser.Text.Trim(); user.Pwd = txtpwd.Text.Trim(); string message = ""; //登录验证用户的输入 if (checklogin(user, ref message)) { //成功跳转到主页 Session["UserInfo"] = user; } else { //失败就提示失败信息 ltl.Text = "登录失败"; } } }
时间: 2024-10-24 21:18:26