C# winform完成上岸次數限制。本站提示廣大學習愛好者:(C# winform完成上岸次數限制)文章只能為提供參考,不一定能成為您想要的結果。以下是C# winform完成上岸次數限制正文
我們在網上上岸的時刻有些網站在用戶屢次輸錯暗碼以後會主動把賬戶解凍,不克不及在停止上岸,小編此次做的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的用法與利益》中曾經寫過。
以上就是本文的全體內容,願望對年夜家的進修有所贊助。