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("账号存在异常,请联系管理员");
}
}
}
}
}
简单的登陆窗口
最新推荐文章于 2024-09-14 21:52:32 发布