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 }

 

posted @ 2018-10-13 14:01  17卓越  阅读(6252)  评论(0编辑  收藏  举报