C# winform實現(xiàn)登陸次數(shù)限制
我們在網(wǎng)上登陸的時候有些網(wǎng)站在用戶多次輸錯密碼之后會自動把賬戶凍結(jié),不能在進行登陸,小編這次做的winform程序就是要實現(xiàn)這種功能,具體內(nèi)容如下
功能一:根據(jù)數(shù)據(jù)庫字段判斷用戶名和密碼是否匹配;
功能二:如果輸入錯誤自動記錄連續(xù)錯誤次數(shù);
功能三:如果用戶登陸成功之后會自動清除錯誤次數(shù),使用戶仍然可以連續(xù)登陸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")); //讀取錯誤登陸次數(shù) if (errortimes >= 3) //判斷錯誤次數(shù)是否大于等于三 { MessageBox.Show("sorry 你已經(jīng)不能再登陸了!"); } else { string passwored = read.GetString(read.GetOrdinal("PassWord")); if (passwored == txbPassWord.Text) { MessageBox.Show("登陸成功!"); this.qingling(); //登陸成功把錯誤登陸次數(shù)清零 } else { MessageBox.Show("登陸失?。?); this.leiji(); //登陸失敗把錯誤登陸次數(shù)加一 } } } } } } }
累加錯誤登陸次數(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(); } } }
清零錯誤登陸次數(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)寫過。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助。
欄 目:C#教程
下一篇:C#獲取串口列表實現(xiàn)實時監(jiān)控串口
本文標題:C# winform實現(xiàn)登陸次數(shù)限制
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6533.html
您可能感興趣的文章
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻 器左下角滾動新聞效果的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實現(xiàn)多線程下載文件的方法
- 01-10C#實現(xiàn)Winform中打開網(wǎng)頁頁面的方法
- 01-10C#實現(xiàn)遠程關(guān)閉計算機或重啟計算機的方法
- 01-10C#自定義簽名章實現(xiàn)方法


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻 器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當前操作系統(tǒng)已
隨機閱讀
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10C#中split用法實例總結(jié)
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10delphi制作wav文件的方法
- 04-02jquery與jsp,用jquery