private void login() { string name = username.Text; string pwd = password.Text; if (name == "" || pwd == "") { MessageBox.Show("用户名或密码不能为空..."); } else { userService userSer = new userService(); User user = new User(); user.LoginName = name; user.Password = pwd; user = userSer.checkLogin(user); if (user == null) { MessageBox.Show("登录失败, 可能错误的消息是" + DBHelper.Message); username.Text = ""; password.Text = ""; username.Focus(); } else { ThirdData.user = user; MainForm main = new MainForm(); main.Show(); this.Hide(); timer1.Enabled = false; } } }
/// <summary> /// 验证用户登录 /// </summary> /// <param name="user"></param> /// <returns></returns> public User checkLogin(User user) { User returnUser = null; string sql = "select * from users where loginName = @name and password = @pwd"; SqlParameter[] value = new SqlParameter[]{ new SqlParameter("@name", user.LoginName), new SqlParameter("@pwd", user.Password) }; DataTable dt = DBHelper.GetDataTable(sql, value); if(dt.Rows.Count > 0) { DataRow dr = dt.Rows[0]; returnUser = new User(); returnUser.LoginName = user.LoginName; returnUser.Password = user.Password; returnUser.Id = Convert.ToInt32(dr["id"]); returnUser.RealName = dr["realName"].ToString(); } return returnUser; }