C# winform实现记住用户登录的登录状态
namespace DMS { public class LoginInfo { public string AccountID { set; get; } public string AccountPassWord { set; get; } public DateTime AccountLoginTime { set; get; } private static LoginInfo _CurrentUser = null; public static LoginInfo CurrentUser { get { if(_CurrentUser == null) { _CurrentUser = new LoginInfo(); } return _CurrentUser; } } } }
貌似是叫单件模式(Singleton)。。。anyway做完这次课程设计就去好好看看设计模式的东西(逃
namespace DMS { public partial class 登录界面 : Form { public 登录界面() { InitializeComponent(); } private void buttonAdmin_Click(object sender, EventArgs e) { if(textBoxUserName.Text.Trim() != "" || textBoxPassWord.Text.Trim() != "") { //连接数据库进行比对…… //进行准备 try//开始比对 { conn.Open(); SqlDataReader reader = cmd.ExecuteReader(); reader.Read(); if (reader.HasRows)//存在账户 { /*下面四行是重点*/ LoginInfo.CurrentUser.AccountID = textBoxUserName.Text.Trim(); LoginInfo.CurrentUser.AccountPassWord = textBoxPassWord.Text.Trim(); LoginInfo.CurrentUser.AccountLoginTime = DateTime.Now; //这里记得得把LoginInfo的实例传进下一个界面不然下一个界面就得不到当前登录信息 管理员父界面 adminMain = new 管理员父界面(LoginInfo.CurrentUser); this.Hide(); adminMain.Show(); } else { MessageBox.Show("用户名或者密码错误!", "提示"); textBoxUserName.Text = ""; textBoxPassWord.Text = ""; textBoxUserName.Focus(); } reader.Close(); } catch (SqlException ex) { MessageBox.Show("DMS出现了一个错误:" + ex.Message); } finally { conn.Close(); } } else MessageBox.Show("用户名或密码为空,请重新输入!", "提示"); } }