黑马程序员 员工管理系统登录 功能实现

 

 1   int errorTimes = 0;
 2 
 3         private void btnOk_Click(object sender, RoutedEventArgs e)
 4         {
 5             if (txtUser.Text.Length <= 0)
 6             {
 7                 MessageBox.Show("请输入用户名!");
 8                 txtUser.Focus();
 9                 return;
10             }
11             if (txtPwd.Password.Length <= 0)
12             {
13                 MessageBox.Show("请输入密码!");
14                 txtPwd.Focus();
15                 return;
16             }
17             EmpInfoWindow empInfo = new EmpInfoWindow();
18 
19             DataTable table = SqlHelper.ExecuteDataTable("select * from T_EmpUser where username=@UserName",
20                 new SqlParameter("@UserName", txtUser.Text));
21 
22             if (table.Rows.Count <= 0)
23             {
24                 MessageBox.Show("用户不存在!");
25                 txtUser.Focus();
26                 txtUser.SelectAll();
27                 return;
28             }
29 
30             //防止意外两个重名用户,做处理。
31             if (table.Rows.Count > 1)
32             {
33                 throw new Exception("用户名重复");
34             }
35 
36             DataRow row = table.Rows[0];
37             string dbPwd = (string)row["password"];
38             long Id = (long)row["id"];
39             errorTimes = (int)row["errortimes"];
40             if (errorTimes >= 3)
41             {
42                 MessageBox.Show("输入次数超过3次,用户已被锁定");
43                 return;
44             }
45             if (dbPwd != txtPwd.Password)
46             {
47                 SqlHelper.ExecuteNonQuery("update T_EmpUser set errortimes = errortimes + 1 where @Id = id",
48                     new SqlParameter("@Id",Id));
49                 MessageBox.Show("密码输入错误!还有" + (3 - errorTimes) + "次输入机会,如果连续输入超过3次,用户将被锁定!");
50                 txtPwd.Focus();
51                 txtPwd.SelectAll();
52                 this.Focus();
53             }
54             else
55             {
56                 this.Hide();
57                 empInfo.ShowDialog();
58             }
59 
60  

 

 

 

posted @ 2013-04-10 17:42  李蒙  阅读(433)  评论(0)    收藏  举报