C winform realizes the limit of login times

  • 2021-09-20 21:19:59
  • OfStack

When we log in on the net, some websites will automatically freeze the account after the user enters the wrong password many times, and can't log in. The winform program made by this site this time is to realize this function, and the specific contents are as follows

Function 1: Judge whether the user name and password match according to the database fields;

Function 2: If the input error automatically records the number of consecutive errors;

Function 3: If the user logs in successfully, it will automatically clear the number of errors, so that the user can still log in for 3 times continuously;

First, drag two label and textbox on the winform form, and textbox is named txbUserName and txbPassWord respectively. Then drag in an button button; Double-click the button button to write the button event, the code is as follows:


private void button1_Click(object sender, EventArgs e)
    {
      using (SqlConnection con = new SqlConnection("server=.; database=text; integrated security=SSPI;"))
      {
        using (SqlCommand com = new SqlCommand())
        {
          com.CommandText = "select * from T_Users where UserName=@username";
          com.Connection = con;
          con.Open();
          com.Parameters.Add(new SqlParameter("username", txbUserName.Text));
          //com.Parameters.Add(new SqlParameter("password", textBox2.Text));
          using (SqlDataReader read = com.ExecuteReader())
          {
            if (read.Read())
            {
              int errortimes = read.GetInt32(read.GetOrdinal("ErrorTimes")); // Read the number of wrong logins 
              if (errortimes >= 3)    // Determine whether the number of errors is greater than or equal to 3
              {
                MessageBox.Show("sorry  You can no longer log in! ");
              }
              else
              {
                string passwored = read.GetString(read.GetOrdinal("PassWord"));
                if (passwored == txbPassWord.Text)
                {
                  MessageBox.Show(" Login succeeded! ");
                  this.qingling();        // Successful login clears the number of wrong logins 
                }
                else
                {
                  MessageBox.Show(" Log in failed! ");
                  this.leiji();        // Login failure adds the number of wrong logins 1
                }
              }
            }
          }
        }
      }
    } 

Accumulate error login times function:


public void leiji()
    {
      using (SqlConnection con = new SqlConnection("server=.; database=text; integrated security=SSPI;"))
      {
        using (SqlCommand com = new SqlCommand())
        {
          com.Connection = con;
          com.CommandText = "update T_Users set ErrorTimes=ErrorTimes+1 where UserName=@username";
          com.Parameters.Add(new SqlParameter("username", txbUserName.Text));
          con.Open();
          com.ExecuteNonQuery();
        }
      } 
    }

Clear error login number function:


 public void qingling()
    {
      using (SqlConnection con = new SqlConnection("server=.; database=text; integrated security=SSPI;"))
      {
        using (SqlCommand com = new SqlCommand())
        {
          com.Connection = con;
          com.CommandText = "update T_Users set ErrorTimes=0 where UserName=@username";
          com.Parameters.Add(new SqlParameter("username", txbUserName.Text));
          con.Open();
          com.ExecuteNonQuery();
        }
      }
    }

This site uses using in the code for the button event, and the usage and benefits of using have been written in "Talking about the Usage and Benefits of C # using".

The above is the whole content of this paper, hoping to help everyone's study.


Related articles: