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.