亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C# winform實(shí)現(xiàn)登陸次數(shù)限制

 更新時(shí)間:2016年05月07日 17:02:37   作者:net小伙  
這篇文章主要介紹了C# winform實(shí)現(xiàn)登陸次數(shù)限制,相信大家都遇到過(guò)網(wǎng)站在用戶(hù)多次輸錯(cuò)密碼之后會(huì)自動(dòng)把賬戶(hù)凍結(jié)的情況,這種功能如何實(shí)現(xiàn),下面小編為大家分享實(shí)現(xiàn)方法

我們?cè)诰W(wǎng)上登陸的時(shí)候有些網(wǎng)站在用戶(hù)多次輸錯(cuò)密碼之后會(huì)自動(dòng)把賬戶(hù)凍結(jié),不能在進(jìn)行登陸,小編這次做的winform程序就是要實(shí)現(xiàn)這種功能,具體內(nèi)容如下

功能一:根據(jù)數(shù)據(jù)庫(kù)字段判斷用戶(hù)名和密碼是否匹配;

功能二:如果輸入錯(cuò)誤自動(dòng)記錄連續(xù)錯(cuò)誤次數(shù);

功能三:如果用戶(hù)登陸成功之后會(huì)自動(dòng)清除錯(cuò)誤次數(shù),使用戶(hù)仍然可以連續(xù)登陸3次;

首先在winform窗體上拖入兩個(gè)label和textbox,textbox分別命名為txbUserName,txbPassWord;然后在拖入一個(gè)button按鈕;雙擊button按鈕寫(xiě)按鈕事件,代碼如下:

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")); //讀取錯(cuò)誤登陸次數(shù)
              if (errortimes >= 3)    //判斷錯(cuò)誤次數(shù)是否大于等于三
              {
                MessageBox.Show("sorry 你已經(jīng)不能再登陸了!");
              }
              else
              {
                string passwored = read.GetString(read.GetOrdinal("PassWord"));
                if (passwored == txbPassWord.Text)
                {
                  MessageBox.Show("登陸成功!");
                  this.qingling();        //登陸成功把錯(cuò)誤登陸次數(shù)清零
                }
                else
                {
                  MessageBox.Show("登陸失?。?);
                  this.leiji();        //登陸失敗把錯(cuò)誤登陸次數(shù)加一
                }
              }
            }
          }
        }
      }
    } 

累加錯(cuò)誤登陸次數(shù)函數(shù):       

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();
        }
      } 
    }

清零錯(cuò)誤登陸次數(shù)函數(shù):       

 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();
        }
      }
    }

在button事件的代碼中小編使用了using,關(guān)于using的用法與好處在《談C# using的用法與好處》中已經(jīng)寫(xiě)過(guò)。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。

相關(guān)文章

最新評(píng)論