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("用戶名或密碼爲空,請從新輸入!", "提示"); } }