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 用户登录
{
public partial class Form1 : Form
{
publicForm1()
{
InitializeComponent();
}
///<summary>
///增加错误次数的方法
///</summary>
privatevoid IncErrorTime()
{
stringConnStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
using(SqlConnection conn = new SqlConnection(ConnStr))
{
using(SqlCommand updateCmd =conn.CreateCommand())
{
updateCmd.CommandText = "update T_Admin set errortime=errortime+1 whereUsername=@username";
updateCmd.Parameters.Add(new SqlParameter("username", txtUserName.Text));
conn.Open();
updateCmd.ExecuteNonQuery();
}
}
}
///<summary>
///错误次数清0
///</summary>
privatevoid NotErrorTime()
{
stringConnStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
using(SqlConnection conn = new SqlConnection(ConnStr))
{
using(SqlCommand updateCmd =conn.CreateCommand())
{
updateCmd.CommandText = "update T_Admin set errortime=0 whereUsername=@username";
updateCmd.Parameters.Add(new SqlParameter("username", txtUserName.Text));
conn.Open();
updateCmd.ExecuteNonQuery();
}
}
}
///<summary>
///密码错误3次,记录当前时间加30分钟
///</summary>
privatevoid IncLoginTime()
{
stringConnStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
using(SqlConnection conn = new SqlConnection(ConnStr))
{
using(SqlCommand updateCmd =conn.CreateCommand())
{
updateCmd.CommandText = "update T_Admin set logintime=@logintime whereUsername=@username";
DateTimelogintime = DateTime.Now.AddMinutes(30);
updateCmd.Parameters.Add(new SqlParameter("username", txtUserName.Text));
updateCmd.Parameters.Add(new SqlParameter("logintime", logintime.ToString()));
conn.Open();
updateCmd.ExecuteNonQuery();
}
}
}
privatevoid button1_Click(objectsender, EventArgs e)
{
stringusername = txtUserName.Text;
stringpassword = txtPassword.Text;
stringConnStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
using(SqlConnection conn = new SqlConnection(ConnStr))
{
using(SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "select * from T_Admin whereusername=@username";
cmd.Parameters.Add(new SqlParameter("username", username));
conn.Open();
using(SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())
{
//用户存在
string dbpassword =reader.GetString(reader.GetOrdinal("password"));
DateTime logintime =reader.GetDateTime(reader.GetOrdinal("logintime"));
//判断当前时间是是服务器允许登录时间
if (logintime > DateTime.Now)
{
MessageBox.Show("一定时间内禁止登录");
return;
}
//如果密码正确
if (dbpassword == txtPassword.Text)
{
NotErrorTime();
MessageBox.Show("登录成功!");
}
//如果密码错误
else
{
int errortime = reader.GetInt32(reader.GetOrdinal("errortime"));
if (errortime >= 2)
{
MessageBox.Show("密码错误次数太多!");
IncLoginTime();
NotErrorTime();
return;
}
MessageBox.Show("密码错误!");
IncErrorTime();//密码错误,次数加1
}
}
else//用户名不存在
{
MessageBox.Show("用户名不存在!");
return;
}
}
}
}
}
}
}