源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

C# winform实现登陆次数限制

  • 时间:2021-12-30 06:03 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:C# winform实现登陆次数限制
我们在网上登陆的时候有些网站在用户多次输错密码之后会自动把账户冻结,不能在进行登陆,小编这次做的winform程序就是要实现这种功能,具体内容如下 [b]功能一:[/b]根据数据库字段判断用户名和密码是否匹配; [b]功能二:[/b]如果输入错误自动记录连续错误次数; [b]功能三:[/b]如果用户登陆成功之后会自动清除错误次数,使用户仍然可以连续登陆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的用法与好处在《[url=http://www.1sucai.cn/article/83730.htm][b]谈C# using的用法与好处[/b][/url]》中已经写过。 以上就是本文的全部内容,希望对大家的学习有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部