好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

C# winform实现登陆次数限制

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

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

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

功能三: 如果用户登陆成功之后会自动清除错误次数,使用户仍然可以连续登陆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的用法与好处在《 谈C# using的用法与好处 》中已经写过。

以上就是本文的全部内容,希望对大家的学习有所帮助。

dy("nrwz");

查看更多关于C# winform实现登陆次数限制的详细内容...

  阅读:51次