C# winform實(shí)現(xiàn)登陸次數(shù)限制
我們?cè)诰W(wǎng)上登陸的時(shí)候有些網(wǎng)站在用戶多次輸錯(cuò)密碼之后會(huì)自動(dòng)把賬戶凍結(jié),不能在進(jìn)行登陸,小編這次做的winform程序就是要實(shí)現(xiàn)這種功能,具體內(nèi)容如下
功能一:根據(jù)數(shù)據(jù)庫(kù)字段判斷用戶名和密碼是否匹配;
功能二:如果輸入錯(cuò)誤自動(dòng)記錄連續(xù)錯(cuò)誤次數(shù);
功能三:如果用戶登陸成功之后會(huì)自動(dòng)清除錯(cuò)誤次數(shù),使用戶仍然可以連續(xù)登陸3次;
首先在winform窗體上拖入兩個(gè)label和textbox,textbox分別命名為txbUserName,txbPassWord;然后在拖入一個(gè)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")); //讀取錯(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)寫過。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
- Oracle 添加用戶并賦權(quán),修改密碼,解鎖,刪除用戶的方法
- Oracle新建用戶、角色,授權(quán),建表空間的sql語(yǔ)句
- [Oracle] 如何使用觸發(fā)器實(shí)現(xiàn)IP限制用戶登錄
- Zend Framework框架的session會(huì)話周期及次數(shù)限制使用示例
- perl腳本實(shí)現(xiàn)限制ssh最大登錄次數(shù)(支持白名單)
- ORACLE 10G修改字符編碼沒有超字符集的限制
- javascript Deferred和遞歸次數(shù)限制實(shí)例
- Java用Cookie限制點(diǎn)贊次數(shù)(簡(jiǎn)版)
- java發(fā)送短信系列之限制日發(fā)送次數(shù)
- Oracle用戶連續(xù)登錄失敗次數(shù)限制如何取消
相關(guān)文章
C#實(shí)現(xiàn)文字轉(zhuǎn)語(yǔ)音功能
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)文字轉(zhuǎn)語(yǔ)音功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
C#中decimal保留2位有效小數(shù)的實(shí)現(xiàn)方法
這篇文章主要介紹了C#中decimal保留2位有效小數(shù)的實(shí)現(xiàn)方法,針對(duì)decimal變量保留2位有效小數(shù)有多種方法,可以使用Math.Round方法以及ToString先轉(zhuǎn)換為字符串等操作來實(shí)現(xiàn)。具體實(shí)現(xiàn)方法感興趣的朋友跟隨小編一起看看吧2019-10-10
C#里SuperSocket庫(kù)不能發(fā)現(xiàn)命令的原因
這篇文章主要介紹C#里SuperSocket庫(kù)不能發(fā)現(xiàn)命令的原因,在使用SuperSocket來寫服務(wù)器的過程中,這是一個(gè)非??焖俚拈_發(fā)方式,也非常好用。不過學(xué)習(xí)的曲線有點(diǎn)高,在使用的過程中經(jīng)常會(huì)遇到各種各樣的問題。下面來看看學(xué)習(xí)舉例說明吧2021-10-10
C# 16 進(jìn)制字符串轉(zhuǎn) int的方法
這篇文章主要介紹了C# 16 進(jìn)制字符串轉(zhuǎn) int的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-04-04
深入多線程之:深入生產(chǎn)者、消費(fèi)者隊(duì)列分析
本篇文章是對(duì)生產(chǎn)者與消費(fèi)者隊(duì)列進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
WinForm中變Enter鍵為Tab鍵實(shí)現(xiàn)焦點(diǎn)轉(zhuǎn)移的方法
這篇文章主要介紹了WinForm中變Enter鍵為Tab鍵實(shí)現(xiàn)焦點(diǎn)轉(zhuǎn)移的方法,主要通過一個(gè)ControlTools類來實(shí)現(xiàn)該功能,需要的朋友可以參考下2014-08-08

