C#在登录界面限制用户登陆次数,下面的程序哪里错了??

栗子hl 2014-05-07 12:07:20
在登陆界面限制用户输错密码的上限为3次,超过三次就自动禁止,可是运行有错误,找不出来,有人可以帮忙看看吗??谢谢

private void btnLogin_Click(object sender, EventArgs e)
{
int count = 0;
if (this.txtUserId.Text == "")
{
this.errWarning.SetError(this.txtUserId, "用户名不能为空!");
return;
}

if (this.txtUserPwd.Text == "")
{
this.errWarning.SetError(this.txtUserPwd, "密码不能为空!");
return;
}
if (this.cmbStatus.Text == "")
{
this.errWarning.SetError(this.cmbStatus, "请选择用户身份!");
return;
}
//判断用户名,密码,身份是否为空

if (sqlCon.State == ConnectionState.Open)
sqlCon.Close();
sqlCon.Open(); //先判断是否已经连接上数据库,若已经连接则先断开再连接,否则直接连接数据库

sqlCmd.CommandText = "select * from tb_Student where fID='" + this.txtUserId.Text + "'and fPwd='" + this.txtUserPwd.Text + "'";
sqlCmd.Connection = sqlCon;
SqlDataReader sqlDr = sqlCmd.ExecuteReader();
if (sqlDr.Read())
{
//MessageBox.Show("登陆成功!"); //测试是否连接成功
frmMain obj = new frmMain(); //实例化主窗体对象
this.Hide();
obj.Show(); //显示主窗体


}
else //出错
{
sqlDr.Close();
// ++count;
MessageBox.Show("用户名或密码错误!,请重新输入", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

String s1 = "select count(*) from tb_Login where fID ='" +this.txtUserId.Text + "'";
sqlCmd.CommandText = s1;
sqlCmd.Connection = sqlCon;
int i = Convert.ToInt32(sqlCmd.ExecuteScalar());
if (i == 0)
{
sqlCmd.CommandText = "insert into tb_Login values('" + this.txtUserId.Text + "'," + ++count + ")";
sqlCmd.Connection = sqlCon;
sqlCmd.ExecuteNonQuery();
return;

}
else if (i > 0 && count <= 3)
{
sqlCmd.CommandText = "update tb_Login set ErrorCount=" + ++count;
//运行时count实现不了增加的操作,一直是1,这是为什么??


sqlCmd.Connection = sqlCon;
sqlCmd.ExecuteNonQuery();
return;

}
else
{
MessageBox.Show("密码输错次数超过三次,已经被禁止输入,请与管理员进行联系");
sqlCmd.CommandText = "delete from tb_Student where fID ='" + this.txtUserId.Text + "'";
sqlCmd.Connection = sqlCon;
sqlCmd.ExecuteNonQuery();
return;
//没有办法实现禁止的效果

}
}

}
...全文
329 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
cindy313 2014-08-01
  • 打赏
  • 举报
回复
threenewbee 2014-05-07
  • 打赏
  • 举报
回复
int count = 0; 要写在函数外面,不然每次执行,都被清0了
栗子hl 2014-05-07
  • 打赏
  • 举报
回复
是全局和局部变量的问题,我明白了。非常感谢各位的帮助
lcawen 2014-05-07
  • 打赏
  • 举报
回复
这是全局变量和局部变量作用域的问题
lbwxly2012 2014-05-07
  • 打赏
  • 举报
回复
每次执行btnLogin_Click方法时,count都被重新赋值为0.
小恒丶 2014-05-07
  • 打赏
  • 举报
回复
[img=http://forum.csd[img=https://forum.csdn.net/PointForum/ui/scripts/csdn/Plugin/003/onion/18.gif]你每点一次 你次数都是0开始啊。。
xingeddx 2014-05-07
  • 打赏
  • 举报
回复
count弄成全局的,可以保存纪录
happytonice 2014-05-07
  • 打赏
  • 举报
回复
sqlCmd.CommandText = "update tb_Login set ErrorCount=" + ++count; 变成: count++ sqlCmd.CommandText = "update tb_Login set ErrorCount=" +count; 禁止的效果可以在登陆三次后,把btnLogin按钮设置为不可用

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧