简单的登陆窗口

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;

namespace 登陆窗口2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string sqlconn = ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
        private void btn_login_Click(object sender, EventArgs e)
        {
            using (SqlConnection conn=new SqlConnection (sqlconn))
            {
                SqlCommand comm = conn.CreateCommand();
                comm.Parameters.AddWithValue("@n", this.txtName.Text);//账号
                comm.Parameters.AddWithValue("@p", this.txtPassword.Text);//密码
                conn.Open();
                //检测错误次数
                comm.CommandText = "select Error from T_users where userName=@n";
                int error =Convert.ToInt32(comm.ExecuteScalar());//错误次数

                //检测间隔时间
                comm.CommandText = "select dateDiff(second,ErrorTime,getdate()) from T_users where username=@n";
                object errortime = comm.ExecuteScalar();//取到两次输入密码的间隔时间
                if (!System.DBNull.Value.Equals(errortime))
                {
                    if (Convert.ToInt32(errortime) > 1200)//间隔大于1200秒时,Error归0
                    {
                        comm.CommandText = "update T_users set Error=0 where userName=@n";
                        comm.ExecuteNonQuery();
                    }
                    else if (Convert.ToInt32(errortime) < 1200 && error >= 3)
                    {
                        MessageBox.Show("连续错误3次,请稍后再试");
                        return;
                    }
                }

                //检测账号密码是否正确
                comm.CommandText = "select count(id) from T_users where userName=@n and password=@p";
                object ok = comm.ExecuteScalar();
                if (Convert.ToInt32(ok)==1)
                {
                    //登陆成功时,清空错误次数
                    comm.CommandText = "update T_users set Error=0 where userName=@n";
                    comm.ExecuteNonQuery();
                    MessageBox.Show("登录成功");
                }
                else if(Convert.ToInt32(ok)==0)
                {
                    //登陆失败时,记录错误次数及错误时间
                    comm.CommandText = "update T_users set Error=Error+1,ErrorTime=getdate() where userName=@n";
                    comm.ExecuteNonQuery();
                    MessageBox.Show("账号或密码错误");
                }
                else
                {
                    MessageBox.Show("账号存在异常,请联系管理员");
                }
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值