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

一.顯示登錄窗口sql

  應用程序入口點爲Main方法,所以在Main方法中建立登錄窗體。數據庫

    1)建立登錄窗體(登錄窗體UI已提早建立好);this

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

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

  具體代碼以下:orm


二.登錄窗體數據訪問方法的編寫對象

  1.準備:blog

    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 }
相關文章
相關標籤/搜索