Chinaunix首页 | 论坛 | 博客
  • 博客访问: 510601
  • 博文数量: 88
  • 博客积分: 2256
  • 博客等级: 大尉
  • 技术积分: 921
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-08 23:20
个人简介

积硅步,行千里

文章分类

全部博文(88)

文章存档

2019年(5)

2018年(1)

2016年(15)

2015年(23)

2013年(3)

2012年(6)

2011年(3)

2010年(22)

2009年(10)

我的朋友

分类: C#/.net

2015-10-29 13:41:18

实例:


  1. private void LoginBtn_Click(object sender, EventArgs e)
  2.         {
  3.             //判断用户名和密码输入情况
  4.             if (UserName.Text.Length <= 0)
  5.             {
  6.                 MessageBox.Show("用户名为空,请输入用户名!");
  7.                 return;
  8.             }
  9.             if (txtPassword.Text.Length <= 0)
  10.             {
  11.                 MessageBox.Show("密码不能为空!");
  12.                 return;
  13.             }

  14.             try
  15.             {
  16.                 //string conn = dbconfig.readxml();方法一
  17.                 string conn = configxml.xmlconn.readxml();
  18.                 //使用xml读取配置进行数据库连接替代20151023--(使用app.config默认配置程序连接数据库配置)

  19.                 //string conn = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
  20.                 //使用db.config就是上面这个方法来写,使用ConfigurationManager但要先引用System.configuration;方法二
  21.                 Helpers.SqlHelper db = new Helpers.SqlHelper(conn);
  22.                 DataTable dt = db.ExecuteDataTable("select Id,username,password,ErrorTimes,sign_time from T_users where username = @userName", new SqlParameter("@username", UserName.Text));

  23.                 //防御性编程,判断过滤条件
  24.                 if (dt.Rows.Count <= 0)
  25.                 {
  26.                     MessageBox.Show("输入的用户名不存在或密码错误,请重新输入!");
  27.                     return;
  28.                 }

  29.                 if (dt.Rows.Count > 1)
  30.                 {
  31.                     MessageBox.Show("用户名重复!");
  32.                     return;
  33.                 }
  34.                 //读取行记录
  35.                 DataRow row = dt.Rows[0];
  36.                 //显性转换类型
  37.                 string dbpassword = (string)row["password"];
  38.                 long Id = (long)row["Id"];
  39.                 int ErrorTimes = (int)row["ErrorTimes"];
  40.                 DateTime dbtime = (DateTime)row["sign_time"];

  41.                 //用Timespan函数计算间隔时间差
  42.                 DateTime TxtTime = DateTime.Parse(DateTime.Now.ToString());
  43.                 TimeSpan timespan = TxtTime - dbtime;

  44.                 if (ErrorTimes >= 5)
  45.                 {
  46.                     if (timespan.TotalMinutes > 5) //如果超过5分钟即解除锁定
  47.                     {
  48.                         db.ExecuteNonQuery("update T_users set ErrorTimes = 0 where Id = @Id", new SqlParameter("@Id", Id));
  49.                     }
  50.                     else
  51.                     {
  52.                         MessageBox.Show("因密码输错次数过多,该账号已经被禁止登录,请与管理员进行联系或稍候再试!");
  53.                     }
  54.                 }
  55.                 else
  56.                 {
  57.                     if (dbpassword != Encrypt.GetMD5_32(txtPassword.Text))
  58.                     {
  59.                         {
  60.                             db.ExecuteNonQuery("update T_users set ErrorTimes = ErrorTimes+1,sign_time = '" + DateTime.Now + "' where Id = @Id", new SqlParameter("@ID", Id));
  61.                             MessageBox.Show("密码错误,请重新输入!");
  62.                             txtPassword.Clear();//输入错误自动清空输入框
  63.                             return;
  64.                         }
  65.                     }
  66.                     else
  67.                     {
  68.                         db.ExecuteNonQuery("update T_users set ErrorTimes = 0 where Id = @Id", new SqlParameter("@Id", Id));
  69.                         GlobalInfo.LoginUser = UserName.Text.ToString();
  70.                         MISMain d = new MISMain();
  71.                         d.Show();
  72.                         this.Hide();
  73.                     }
  74.                 }
  75.             }

  76.             catch
  77.             {
  78.                 ServerConfig sc = new ServerConfig();
  79.                 sc.Show();
  80.             }
  81.         }

阅读(1036) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~