zoukankan      html  css  js  c++  java
  • 【C#】登陆次数限制

      我们在网上登陆的时候有些网站在用户多次输错密码之后会自动把账户冻结,不能在进行登陆,笔者这次做的winform程序就是要实现这种功能。

      功能一:根据数据库字段判断用户名和密码是否匹配;

      功能二:如果输入错误自动记录连续错误次数;

      功能三:如果用户登陆成功之后会自动清除错误次数,使用户仍然可以连续登陆3次;

      首先在winform窗体上拖入两个label和textbox,textbox分别命名为txbUserName,txbPassWord;然后在拖入一个button按钮;双击button按钮写按钮事件,代码如下:

     1         private void button1_Click(object sender, EventArgs e)
     2         {
     3             using (SqlConnection con = new SqlConnection("server=.; database=text; integrated security=SSPI;"))
     4             {
     5                 using (SqlCommand com = new SqlCommand())
     6                 {
     7                     com.CommandText = "select * from T_Users where UserName=@username";
     8                     com.Connection = con;
     9                     con.Open();
    10                     com.Parameters.Add(new SqlParameter("username", txbUserName.Text));
    11                     //com.Parameters.Add(new SqlParameter("password", textBox2.Text));
    12                     using (SqlDataReader read = com.ExecuteReader())
    13                     {
    14                         if (read.Read())
    15                         {
    16                             int errortimes = read.GetInt32(read.GetOrdinal("ErrorTimes"));  //读取错误登陆次数
    17                             if (errortimes >= 3)        //判断错误次数是否大于等于三
    18                             {
    19                                 MessageBox.Show("sorry 你已经不能再登陆了!");
    20                             }
    21                             else
    22                             {
    23                                 string passwored = read.GetString(read.GetOrdinal("PassWord"));
    24                                 if (passwored == txbPassWord.Text)
    25                                 {
    26                                     MessageBox.Show("登陆成功!");
    27                                     this.qingling();                //登陆成功把错误登陆次数清零
    28                                 }
    29                                 else
    30                                 {
    31                                     MessageBox.Show("登陆失败!");
    32                                     this.leiji();               //登陆失败把错误登陆次数加一
    33                                 }
    34                             }
    35                         }
    36                     }
    37                 }
    38             }
    39         }
    累加错误登陆次数函数
     1         public void leiji()
     2         {
     3             using (SqlConnection con = new SqlConnection("server=.; database=text; integrated security=SSPI;"))
     4             {
     5                 using (SqlCommand com = new SqlCommand())
     6                 {
     7                     com.Connection = con;
     8                     com.CommandText = "update T_Users set ErrorTimes=ErrorTimes+1 where UserName=@username";
     9                     com.Parameters.Add(new SqlParameter("username", txbUserName.Text));
    10                     con.Open();
    11                     com.ExecuteNonQuery();
    12                 }
    13             } 
    14         }
    清零错误登陆次数函数
     1         public void qingling()
     2         {
     3             using (SqlConnection con = new SqlConnection("server=.; database=text; integrated security=SSPI;"))
     4             {
     5                 using (SqlCommand com = new SqlCommand())
     6                 {
     7                     com.Connection = con;
     8                     com.CommandText = "update T_Users set ErrorTimes=0 where UserName=@username";
     9                     com.Parameters.Add(new SqlParameter("username", txbUserName.Text));
    10                     con.Open();
    11                     com.ExecuteNonQuery();
    12                 }
    13             }
    14         }

      在button事件的代码中笔者使用了using 关于using的用法与好处在【C#】using用法中已经写过。

  • 相关阅读:
    React16+Redux 实战企业级大众点评Web App
    一类图上二选一构造问题
    O(1)判断两点之间是否有边
    ARC112F Die Siedler
    【学习笔记】同余最短路
    CF1034D Intervals of Intervals
    CF1034C Region Separation
    CF650E Clockwork Bomb
    莫队题三道(LOJ6273, CF1476G, CF700D)
    CF1290D Coffee Varieties (hard version)
  • 原文地址:https://www.cnblogs.com/ngnetboy/p/2519269.html
Copyright © 2011-2022 走看看