欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

WinForm登录验证

程序员文章站 2022-07-02 17:37:58
概述:输错三次禁止登陆,15分钟后才能继续。图示:Form1代码:using System;using System.Configuration;using System.Data.SqlClient;using System.Windows.Forms;namespace 登录验证项目{ publ... ......

概述:输错三次禁止登陆,15分钟后才能继续。

图示:

form1代码:

using system;
using system.configuration;
using system.data.sqlclient;
using system.windows.forms;

namespace 登录验证项目
{
     public partial class form1 : form
     {
         public form1()
         {
             initializecomponent();
         }

        private void button1_click(object sender, eventargs e)
         {
             //连接数据库
             //将连接字符串写入配置文件中 string connstr = "sever=.;uid=sa;pwd=123456;database=demo";

这部分写在配置文件中

//<connectionstrings>
   // <add name="sqlconn" connectionstring="server=.;uid=sa;pwd=123456;database=loginblock"/>
  //</connectionstrings>


             //条添加引用引入configuration命名空间
             string connstr = configurationmanager.connectionstrings["sqlconn"].connectionstring;
             using (sqlconnection conn = new sqlconnection(connstr))
             {
                 using (sqlcommand cmd = conn.createcommand())
                 {

                    conn.open();
                     //select 查询语句str
                     string str = @"select  [uid]
                                               ,[unm]
                                               ,[pwd]
                                               ,[errordate]
                                               ,[errortimes]
                                           from [login_block]
                                           where unm='" + textbox1.text.trim()
                                           + "' and pwd='" + textbox2.text.trim() + "' ";
                     cmd.commandtext = str;
                     // bool ishasdata=false;

//这里创建login对象


                     login login = null;
                     using (sqldatareader reader = cmd.executereader())
                     {//判断是否有数据
                         #region myregion
                         //if (!reader.hasrows)
                         //{
                         //    //修改错误时间,错误次数

                        //}
                         #endregion
                         #region myregion
                         if (reader.hasrows)
                         {
                             reader.read();
                         }
                         #endregion
                         if (reader.read())
                         {
                             login = new login();
                             login.uid = int.parse(reader["uid"].tostring());
                             login.pwd = reader["pwd"].tostring();
                             login.errortimes = int.parse(reader["errortimes"].tostring());
                             login.errordata = datetime.parse(reader["errordate"].tostring());
                         }
                         // ishasdata = reader.hasrows;


                     }//花括号执行结束之前reader对象一直占用conn对象
                     if (login == null/*!ishasdata*/)
                     {
                         //修改 错误时间,错误次数  where username=txtusername.text
                         cmd.commandtext =
                             "update login_block set errordate=getdate(), errortimes=errortimes+1 where unm='" +
                             textbox1.text.trim() + "'";
                         cmd.executenonquery();
                         //messagebox.show("用户名密码error");
                         return;
                     }
                     //执行有数据的过程
                     if (login.errortimes < 3 || datetime.now.subtract(login.errordata).minutes > 15)
                     {
                         messagebox.show("登录成功!!");
                         cmd.commandtext = "update login_block set errortimes=0,errordate=getdate() where uid=" + login.uid;
                         cmd.executenonquery();
                     }
                     else
                     {
                         messagebox.show("登录失败!账号被锁定");
                     }
                 }
             }
         }
     }
}

login类:

using system;

namespace 登录验证项目
{
     public class login
     {
         //[uid],[pwd],[errortimes],[errordata]
         public int uid { get; set; }
         public string unm { get; set; }
         public string pwd { get; set; }
         public int errortimes { get; set; }
         public datetime errordata { get; set; }
     }
}

数据库:

表:login_block

字段:

[uid]                                       

[unm]                                       

[pwd]                                      

[errordate]                                           

[errortimes]