【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用法中已經寫過。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM