Chinaunix首页 | 论坛 | 博客
  • 博客访问: 513085
  • 博文数量: 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-04-27 13:13:56


  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(Password.Text.Length<=0)
  10.             {
  11.                 MessageBox.Show("密码不能为空!");
  12.                 return;
  13.             }
  14.             string conn = @"server=datasvr;uid=mis;password=mis_2015;database=MIS";
  15.             Helpers.SqlHelper db = new Helpers.SqlHelper(conn);


  16.             //登录方法一
  17.             //string sql = @"select UserName,PassWord from dbo.Users";
  18.             //DataTable dt = db.ExecuteDataTable("select username,password from T_users where username = @userName",new SqlParameter("@username",UserName.Text));


  19.             //String userName = (this.UserName.Text).Trim();
  20.             //String passWord = (this.Password.Text).Trim();
  21.             //for (int i = 0; i < dt.Rows.Count; i++)
  22.             //{
  23.             // if (userName.Equals(dt.Rows[i][0].ToString()))
  24.             // {
  25.             // if (passWord.Equals(dt.Rows[i][1].ToString()))
  26.             // {
  27.             // MISMain s = new MISMain();
  28.             // s.Show();
  29.             // this.Hide();
  30.             // //MessageBox.Show("登录成功!");
  31.             // return;
  32.             // }
  33.             // else
  34.             // {
  35.             // MessageBox.Show("密码错误,请重新输入!");
  36.             // return;
  37.             // }


  38.             // }
  39.             // else
  40.             // {
  41.             // MessageBox.Show("用户名或密码输入错误,请输入!");
  42.             // return;
  43.             // }
  44.             //}

  45.             //方法二
  46.              DataTable dt = db.ExecuteDataTable("select Id,username,password,ErrorTimes,sign_time from T_users where username = @userName",new SqlParameter("@username",UserName.Text));
  47.             
  48.             //防御性编程,判断过滤条件是否有
  49.             try
  50.             {
  51.                 if (dt.Rows.Count <= 0)
  52.                 {
  53.                     MessageBox.Show("用户名或密码错误,请输入!");
  54.                 }


  55.                 if (dt.Rows.Count > 1)
  56.                 {
  57.                     MessageBox.Show("用户名重复!");
  58.                 }


  59.                 DataRow row = dt.Rows[0];
  60.                 //显性转换类型
  61.                 string dbpassword = (string)row["password"];
  62.                 long Id = (long)row["Id"];
  63.                 int ErrorTimes = (int)row["ErrorTimes"];
  64.                 DateTime dbtime = (DateTime)row["sign_time"];


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

  68.                 if (ErrorTimes >= 5)
  69.                 {
  70.                     if (timespan.TotalMinutes > 3) //如果超过1分钟即解除锁定
  71.                     {
  72.                         db.ExecuteNonQuery("update T_users set ErrorTimes = 0 where Id = @Id", new SqlParameter("@Id", Id));
  73.                     }
  74.                     else
  75.                     {
  76.                         MessageBox.Show("因密码输错次数过多,该账号已经被禁止登录,请与管理员进行联系或稍候再试!");
  77.                     }
  78.                 }
  79.                 else
  80.                 {
  81.                     if(dbpassword != Password.Text.ToString())
  82.                     {
  83.                         {
  84.                             db.ExecuteNonQuery("update T_users set ErrorTimes = ErrorTimes+1,sign_time = '"+ DateTime.Now+ "' where Id = @Id", new SqlParameter("@ID", Id));
  85.                         }


  86.                          MessageBox.Show("密码错误,请重新输入!");
  87.                     }
  88.                     else
  89.                     {
  90.                                 MISMain d = new MISMain();
  91.                                 d.Show();
  92.                                 this.Hide();
  93.                                 db.ExecuteNonQuery("update T_users set ErrorTimes = 0 where Id = @Id", new SqlParameter("@ID", Id));
  94.                     }
  95.                 }


  96.             }


  97.             catch(SqlException ex)
  98.             {
  99.                 MessageBox.Show(ex.Message);
  100.             }
  101.         }


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