學習登入視窗與主視窗的運行流程,登入視窗流程

來源:互聯網
上載者:User

學習登入視窗與主視窗的運行流程,登入視窗流程

轉載請註明出自朱朱家園http://blog.csdn.net/zhgl7688

總結下互相學慣用:

顯示登入視窗—》判斷登入視窗返回值—》登入成功顯示主視窗,否則關閉程式。

主要代碼如下:

1、在Main中修改部分代碼如下:其中FrmUserLogin為登入視窗;FrmMain為主視窗;

過程:先顯示登入視窗,根據登入視窗返回的result判斷成功與否,成功顯示主視窗,否則關閉程式。

 static void Main()        {            Application.EnableVisualStyles();            Application.SetCompatibleTextRenderingDefault(false);             //顯示登入表單            FrmUserLogin objUserFrm = new FrmUserLogin();           DialogResult result= objUserFrm.ShowDialog();            //判斷登入表單傳回值            if (result == DialogResult.OK)//登入成功               Application.Run(new FrmMain());           else               Application.Exit();//退出整個程式     }

2、登入視窗用到兩個類:管理員資料訪問類AdminService和管理員實體類Admin

2.1管理員類Admin:用於存放管理員資料以方便類之間傳遞用,更方便以後擴充成員用。

類中用到三個成員:登入Id(LoginId),登入密碼(Loginpwd),管理員名稱(AdminName)。

    public class Admin    {        public int LoginId { get; set; }        public string Loginpwd { get; set; }        public string AdminName { get; set; }    }
       2.2管理員資料訪問類AdminService:用於判斷登入資訊在資料庫是否存在。其中用到類靜態方法SQLHelper.GetReader

過程:建立查詢語句sql,在資料庫Admins中查詢loginid=登入Id和loginpwd=登入密碼,返回管理員名稱。

public class AdminService    {        public Admin AdminLogin(Admin objAdmin)        {            string sql = "select AdminName from Admins where loginid={0} and loginpwd={1}";            sql = string.Format(sql, objAdmin.LoginId, objAdmin.Loginpwd);            SqlDataReader objReader = SQLHelper.GetReader(sql);            if (objReader.Read())            {                objAdmin.AdminName = objReader["AdminName"].ToString();            }            else            { objAdmin = null; }            objReader.Close();            return objAdmin;        }    }

2.2.1類靜態方法SQLHelper.GetReader

  public static  SqlDataReader GetReader(string sql)        {            SqlConnection conn = new SqlConnection(connString);            SqlCommand cmd = new SqlCommand(sql, conn);            try            {                conn.Open();                return cmd.ExecuteReader(CommandBehavior.CloseConnection) ;            }            catch (Exception ex)            {                throw ex;            }        }


3、登入視窗中判斷過程: 返回主要語句 this.DialogResult = DialogResult.OK;

  private void btnLogin_Click(object sender, EventArgs e)        {            if (this.txtLoginId.Text.Trim().Length == 0)            {                 MessageBox.Show("請輸入使用者名稱","提示");                this.txtLoginId.Focus();                return;            }            if (this.txtLoginPwd .Text.Trim().Length == 0)            {                MessageBox.Show("請輸入密碼", "提示");                this.txtLoginPwd .Focus();                return;            }            Admin objAdmin = new Admin()            {                LoginId = Convert.ToInt32(txtLoginId.Text),                Loginpwd = txtLoginPwd.Text.Trim()            };            try            {                objAdmin = new AdminService().AdminLogin(objAdmin);                if (objAdmin != null)                {                    Program.objCurrentAdmin = objAdmin;                    this.DialogResult = DialogResult.OK;                    this.Close();                }                else                {                    MessageBox.Show("登入帳號或密碼錯誤!","登入提示");                }            }            catch (Exception ex )            {                MessageBox.Show("資料訪問出現錯誤"+ex.Message );            }        }

以上代碼摘自Andy講師課件中




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.