c#桌面應用程序開發--登陸窗口


一.顯示登陸窗口

  應用程序入口點為Main方法,因此在Main方法中創建登陸窗體。

    1)創建登陸窗體(登陸窗體UI已提前創建好);

    2)顯示窗體,以模式對話框的形式顯示,並賦值給result;

    3)判斷窗體的返回值是否為OK,若是,則顯示主窗體,(窗體的對話框結果在相應的窗體中設置,已達到邏輯處理,登陸驗證的效果),否則退出程序;

  具體代碼如下:


二.登陸窗體數據訪問方法的編寫

  1.准備:

    1)數據訪問層DAL創建:解決方案→新建項目→類庫;

    2)在DAL中創建管理員數據訪問類SysAdminService: DAL→右鍵→類

    3)編寫通用數據訪問類:負責連接數據庫(最基本的格式化SQL語句通用數據訪問類),代碼如下

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Data;
 6 using System.Data.SqlClient;
 7 using System.Configuration;
 8 
 9 namespace DAL
10 {
11     /// <summary>
12     /// 通用數據訪問類
13     /// </summary>
14     class SQLHelper
15     {
16         private static string connString = Common.StringSecurity.DESDecrypt(ConfigurationManager.ConnectionStrings["connString"].ToString());//於數據庫連接的字符串(配置文件解密)
17 
18         /// <summary>
19         /// 執行增、刪、改操作
20         /// </summary>
21         /// <param name="sql"></param>
22         /// <returns></returns>
23         public static int Update(string sql)
24         {
25             SqlConnection conn = new SqlConnection(connString);
26             SqlCommand cmd = new SqlCommand(sql, conn);
27             try
28             {
29                 conn.Open();
30                 return cmd.ExecuteNonQuery();
31             }
32             catch (Exception ex)
33             {
34 
35                 throw ex;
36             }
37             finally
38             {
39                 conn.Close();
40             }
41         }
42 
43         /// <summary>
44         /// 執行單一結果查詢
45         /// </summary>
46         /// <param name="sql"></param>
47         /// <returns></returns>
48         public static object GetSingleResult(string sql)
49         {
50             SqlConnection conn = new SqlConnection(connString);
51             SqlCommand cmd = new SqlCommand(sql, conn);
52             try
53             {
54                 conn.Open();
55                 return cmd.ExecuteScalar();
56             }
57             catch (Exception ex)
58             {
59 
60                 throw ex;
61             }
62             finally
63             {
64                 conn.Close();
65             }
66         }
67 
68         /// <summary>
69         /// 返回結果集的查詢
70         /// </summary>
71         /// <param name="sql"></param>
72         /// <returns></returns>
73         public static SqlDataReader GetReader(string sql)
74         {
75             SqlConnection conn = new SqlConnection(connString);
76             SqlCommand cmd = new SqlCommand(sql, conn);
77             try
78             {
79                 conn.Open();
80                 return cmd.ExecuteReader(CommandBehavior.CloseConnection);
81             }
82             catch (Exception ex)
83             {
84                 conn.Close();
85                 throw ex;
86             }
87         }
88 
89     }
90 }

  2.登陸窗體數據訪問方法編寫:

 1 using System.Data.SqlClient;
 2 using Models;
 3 
 4 namespace DAL
 5 {
 6     /// <summary>
 7     /// 管理員數據訪問類
 8     /// </summary>
 9     public class SysAdminService
10     {
11         /// <summary>
12         /// 根據賬號和密碼返回登陸結果的查詢,
13         /// </summary>
14         /// <param name="objAdmin"></param>
15         /// <returns>返回管理員對象,若為空,則表示賬號或密碼錯誤</returns>
16         public SysAdmin AdminLogin(SysAdmin objAdmin)
17         {
18             string sql = "select AdminName from Admins where LoginId={0} and LoginPwd={1}";
19             sql = string.Format(sql, objAdmin.LoginId, objAdmin.LoginPwd);
20 
21             SqlDataReader objReader = SQLHelper.GetReader(sql);
22             if (objReader.Read())//從數據庫查到結果,則表示登陸賬號和密碼正確,將管理員姓名封裝到對象中,並返回對象,以便以后修改賬號密碼使用
23             {
24                 objAdmin.AdminName = objReader["AdminName"].ToString();
25             }
26             else objAdmin = null;//沒查到數據,表示登陸不成功,則清空對象
27             objReader.Close();
28             return objAdmin;
29         }
30     }
31 }

   3.前台UI邏輯編寫(事件+控件)

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Windows.Forms;
 9 using DAL;
10 using Models;
11 
12 
13 namespace StudentManager
14 {
15     public partial class FrmUserLogin : Form
16     {
17         SysAdminService objAdminService = new SysAdminService();
18 
19         public FrmUserLogin()
20         {
21             InitializeComponent();
22         }
23 
24 
25         //登錄
26         private void btnLogin_Click(object sender, EventArgs e)
27         {
28             //[1]數據驗證
29             if (this.txtLoginId.Text.Trim().Length == 0)
30             {
31                 this.lblMsg.Text = "請輸入登陸賬號!";
32                 return;
33             }
34             if (this.txtLoginPwd.Text.Trim().Length == 0)
35             {
36                 this.lblMsg.Text = "請輸入登陸密碼!";
37                 return;
38             }
39 
40             //[2]封裝對象
41             SysAdmin objAdmin = new SysAdmin()
42             {
43                 LoginId = Convert.ToInt32(this.txtLoginId.Text.Trim()),
44                 LoginPwd = this.txtLoginPwd.Text.Trim()
45             };
46             //[3]和后台交互,判斷登陸信息是否正確
47             try
48             {
49                 objAdmin = objAdminService.AdminLogin(objAdmin);
50                 if (objAdmin != null)
51                 {
52                     //保存登陸信息
53                     Program.objCurrentAdmin = objAdmin;
54                     this.DialogResult = DialogResult.OK;//this代表當前窗體
55                     this.Close();
56                 }
57                 else
58                 {
59                     this.lblMsg.Text = "賬號或密碼錯誤!";
60                 }
61             }
62             catch (Exception ex)
63             {
64 
65                 MessageBox.Show("數據訪問出現異常,登陸失敗!具體原因:"+ex.Message);
66             }
67             
68         }
69         //關閉
70         private void btnClose_Click(object sender, EventArgs e)
71         {
72             this.Close();
73         }
74 
75         #region 改善用戶體驗
76         private void txtLoginId_KeyDown(object sender, KeyEventArgs e)
77         {
        //按回車健代替鼠標單擊事件
78 if(e.KeyValue==13) 79 { 80 if(this.txtLoginId.Text.Trim().Length != 0) 81 { 82 this.txtLoginPwd.Focus(); 83 } 84 } 85 } 86 87 private void txtLoginPwd_KeyDown(object sender, KeyEventArgs e) 88 { 89 if(e.KeyValue==13) 90 { 91 btnLogin_Click(null,null); 92 } 93 } 94 95 #endregion 96 97 98 } 99 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM