国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C# winform實現登陸次數限制

2020-01-24 01:10:18
字體:
來源:轉載
供稿:網友

我們在網上登陸的時候有些網站在用戶多次輸錯密碼之后會自動把賬戶凍結,不能在進行登陸,小編這次做的winform程序就是要實現這種功能,具體內容如下

功能一:根據數據庫字段判斷用戶名和密碼是否匹配;

功能二:如果輸入錯誤自動記錄連續錯誤次數;

功能三:如果用戶登陸成功之后會自動清除錯誤次數,使用戶仍然可以連續登陸3次;

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

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

累加錯誤登陸次數函數:       

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

清零錯誤登陸次數函數:       

 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,關于using的用法與好處在《談C# using的用法與好處》中已經寫過。

以上就是本文的全部內容,希望對大家的學習有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 海兴县| 清流县| 武夷山市| 贺兰县| 茌平县| 天津市| 东山县| 晋宁县| 贵阳市| 怀化市| 库尔勒市| 绥滨县| 上思县| 绥德县| 德州市| 漠河县| 房产| 新平| 双鸭山市| 奉贤区| 阿拉善右旗| 乳源| 卢湾区| 南木林县| 兴和县| 孟村| 武陟县| 航空| 鹤峰县| 元朗区| 文登市| 永兴县| 那曲县| 郯城县| 黄石市| 大城县| 正镶白旗| 图木舒克市| 宁夏| 油尖旺区| 岚皋县|