zoukankan      html  css  js  c++  java
  • 【C#】登陆次数限制

      我们在网上登陆的时候有些网站在用户多次输错密码之后会自动把账户冻结,不能在进行登陆,笔者这次做的winform程序就是要实现这种功能。

      功能一:根据数据库字段判断用户名和密码是否匹配;

      功能二:如果输入错误自动记录连续错误次数;

      功能三:如果用户登陆成功之后会自动清除错误次数,使用户仍然可以连续登陆3次;

      首先在winform窗体上拖入两个label和textbox,textbox分别命名为txbUserName,txbPassWord;然后在拖入一个button按钮;双击button按钮写按钮事件,代码如下:

     1         private void button1_Click(object sender, EventArgs e)
     2         {
     3             using (SqlConnection con = new SqlConnection("server=.; database=text; integrated security=SSPI;"))
     4             {
     5                 using (SqlCommand com = new SqlCommand())
     6                 {
     7                     com.CommandText = "select * from T_Users where UserName=@username";
     8                     com.Connection = con;
     9                     con.Open();
    10                     com.Parameters.Add(new SqlParameter("username", txbUserName.Text));
    11                     //com.Parameters.Add(new SqlParameter("password", textBox2.Text));
    12                     using (SqlDataReader read = com.ExecuteReader())
    13                     {
    14                         if (read.Read())
    15                         {
    16                             int errortimes = read.GetInt32(read.GetOrdinal("ErrorTimes"));  //读取错误登陆次数
    17                             if (errortimes >= 3)        //判断错误次数是否大于等于三
    18                             {
    19                                 MessageBox.Show("sorry 你已经不能再登陆了!");
    20                             }
    21                             else
    22                             {
    23                                 string passwored = read.GetString(read.GetOrdinal("PassWord"));
    24                                 if (passwored == txbPassWord.Text)
    25                                 {
    26                                     MessageBox.Show("登陆成功!");
    27                                     this.qingling();                //登陆成功把错误登陆次数清零
    28                                 }
    29                                 else
    30                                 {
    31                                     MessageBox.Show("登陆失败!");
    32                                     this.leiji();               //登陆失败把错误登陆次数加一
    33                                 }
    34                             }
    35                         }
    36                     }
    37                 }
    38             }
    39         }
    累加错误登陆次数函数
     1         public void leiji()
     2         {
     3             using (SqlConnection con = new SqlConnection("server=.; database=text; integrated security=SSPI;"))
     4             {
     5                 using (SqlCommand com = new SqlCommand())
     6                 {
     7                     com.Connection = con;
     8                     com.CommandText = "update T_Users set ErrorTimes=ErrorTimes+1 where UserName=@username";
     9                     com.Parameters.Add(new SqlParameter("username", txbUserName.Text));
    10                     con.Open();
    11                     com.ExecuteNonQuery();
    12                 }
    13             } 
    14         }
    清零错误登陆次数函数
     1         public void qingling()
     2         {
     3             using (SqlConnection con = new SqlConnection("server=.; database=text; integrated security=SSPI;"))
     4             {
     5                 using (SqlCommand com = new SqlCommand())
     6                 {
     7                     com.Connection = con;
     8                     com.CommandText = "update T_Users set ErrorTimes=0 where UserName=@username";
     9                     com.Parameters.Add(new SqlParameter("username", txbUserName.Text));
    10                     con.Open();
    11                     com.ExecuteNonQuery();
    12                 }
    13             }
    14         }

      在button事件的代码中笔者使用了using 关于using的用法与好处在【C#】using用法中已经写过。

  • 相关阅读:
    Python:在一个moudle如何引入另一个moudle下面的包
    RobotFramework做自动化中,能定位到iFrame里面,怎么在iFrame里面输入文字?
    RobotFramework框架做自动化的过程中,遇到不能选择下拉框问题
    雕刻机虚拟仿真及上位机设计(Labview上位机+Proteus仿真)
    医学成像原理——NMR/MRI成像基础
    数字图像处理(二) 灰度变换与空域滤波
    数字图像处理(一) 数字图像基础
    Python爬虫笔记
    python数据分析-第一周
    pyhton-web开发
  • 原文地址:https://www.cnblogs.com/ngnetboy/p/2519269.html
Copyright © 2011-2022 走看看