C # WinForm to achieve the limit of landing times _c# Tutorial

Source: Internet
Author: User

When we landed on the Web some sites in the user many times after the wrong password will automatically freeze the account, can not be landed, small series this time to do the WinForm program is to achieve this function, specific content as follows

function One: according to the database field to determine whether the user name and password match;

function Two: if the input error automatically record the number of consecutive errors;

function Three: If the user login successfully will automatically clear the number of errors, so that users can still log 3 consecutive times;

First drag the two labels and textbox,textbox on the WinForm form to be named Txbusername,txbpassword, and then drag in a button button, and double-click the button button to write an event with the following code:

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 Error login if (errortimes >= 3)//Determine if the number of errors is greater than or equal to three {MessageBox.Show ("Sorry You can no longer log in!
              "); else {string passwored = read. GetString (read.
                GetOrdinal ("PassWord")); If (passwored = = Txbpassword.text) {MessageBox.Show ("Landing success!")
                  ");        This.qingling (); Landing successfully the error landing number 0} else {MessageBox.Show ("Landing failed!")
                  ");        This.leiji ();  Login failed to log in the wrong number of times Plus}}}}

Cumulative error Landing number 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 ();}} 
    

Qing 0 Error Landing 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 ();}}
    

A using using is used in the code for the button event, and the usage and benefits of using are already written in " talking about C # using usage and benefits ."

The above is the entire content of this article, I hope to help you learn.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.