手机
当前位置:查字典教程网 >编程开发 >C#教程 >C# winform实现登陆次数限制
C# winform实现登陆次数限制
摘要:我们在网上登陆的时候有些网站在用户多次输错密码之后会自动把账户冻结,不能在进行登陆,小编这次做的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的用法与好处》中已经写过。

以上就是本文的全部内容,希望对大家的学习有所帮助。

【C# winform实现登陆次数限制】相关文章:

C#多维数组学习使用

C#获取系统版本信息方法

C# DataTable的详细用法分享

c# n个数排序实现代码

C# 去除首尾字符或字符串的方法

C# 撒列实现关键字过滤的实例

c#循环中产生伪随机数

c# winform多线程的小例子

关于C#生成MongoDB中ObjectId的实现方法

C# HttpClient Cookie验证解决方法

精品推荐
分类导航