ADO之密码验证--3次错误就锁定『改进』

这里使用了SqlHelper,简化程序

  自己写一个SqlHelper,把数据库的连接等都写到里面去。

  首先把连接字符串添加到配置文件里去,右键解决方案-->添加新建项-->选择应用程序配置文件

  添加一个下字段,红色部分为新添加的代码,name字段一定要写,下面的使用就是靠这个关键字

1 <?xml version="1.0"?>
2 <configuration>
3   <connectionStrings>
4     <add name="dbconn"
5           connectionString="Data Source=.;Initial Catalog=Test1;User ID=sa;Password=123456"/>
6   
7   </connectionStrings>
8 </configuration>

  在sqlHelper类中添加一个变量,并定义函数

 1  private static string conStr = ConfigurationManager.ConnectionStrings["dbConn"].ConnectionString;
 2 
 3   public static DataTable ExecuteDataTable(string sql, params SqlParameter[] parameters)
 4         {
 5             using (SqlConnection conn = new SqlConnection(conStr))
 6             {
 7                 conn.Open();
 8                 using (SqlCommand cmd = conn.CreateCommand())
 9                 {
10                     cmd.CommandText = sql;
11                     cmd.Parameters.AddRange(parameters);
12                     DataSet dataset = new DataSet();
13                     SqlDataAdapter apdater = new SqlDataAdapter(cmd);
14                     apdater.Fill(dataset);
15                     return dataset.Tables[0];
16                 }
17             }
18         }

  这些操作只是把代码给提出来,不用每次使用都去写下所有代码。

  然后就是函数响应了,非常简洁

 1   private void btn_Login_Click(object sender, RoutedEventArgs e)
 2         {
 3             if (tb_UserID.Text.Length <= 0)
 4             {
 5                 MessageBox.Show("请输入用户名");
 6                 return;
 7             }
 8             if (pwdPassword.Password.Length <= 0)
 9             {
10                 MessageBox.Show("请输入密码");
11                 return;
12             }
13 
14             DataTable table = SqlHelper.ExecuteDataTable("select * from USERINFO where UserID=@user", new SqlParameter("@user", tb_UserID.Text));
15            
16             if (table.Rows.Count <= 0)
17             {
18                 MessageBox.Show("用户不存在");
19                 return;
20             }
21             //要对不可能发生的情况进行做处理,断言 Assert 
22             else if (table.Rows.Count > 1)
23             {
24                 throw new Exception("用户名重复!");
25             }
26 
27             DataRow row = table.Rows[0];
28             string dbPassword = row["Password"].ToString();
29             long id = (long)row["ID"];//获取ID,这是数据库新增加的一个标识字段,没有实际意义,但可以用来数据判断
30             int errorTimes = (int)row["ErrorTimes"];//获取错误次数
31             if (errorTimes >= 3)
32             {
33                 MessageBox.Show("输入次数过多,用户已锁定");
34                 return;
35             }
36             if (dbPassword != pwdPassword.Password)
37             {
38                 //把登录用户的错误次数加一
39                 SqlHelper.ExecuteNonQuery("update USERINFO set ErrorTimes=ErrorTimes+1 where ID=@id"
40                     ,new SqlParameter("@id",id));
41                     
42                 MessageBox.Show("密码错误!");
43             } 
44             else
45             {
46                 MessageBox.Show("登录成功!");    
47             }
48 }

  感觉这样一写,比上个程序简单很多,bug也要少很多。

原文地址:https://www.cnblogs.com/songliquan/p/3648668.html