基于数据库登录判断。
基于MSSQL数据库登录判断,如果密码连续输出3次,则提示用户3分钟后重试。
数据库:
Program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 登录
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
string dataDir = AppDomain.CurrentDomain.BaseDirectory; //将当前路径赋值给 dataDir 变量。
if (dataDir.EndsWith(@"\bin\Debug\") || dataDir.EndsWith(@"\bin\Release")) //判断 dataDir 字符串结尾 是不是依这两个中的任意一个结尾的。
{
dataDir = System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName; //将 dataDir 变量中的路径 向上两级,赋值给 dataDir。
AppDomain.CurrentDomain.SetData("DataDirectory", dataDir); //最后将路径 赋值给 DataDirectory.
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}Form1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 登录
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void 错误次数增加() //增加错误次数方法
{
using (SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDBFilename=|DataDirectory|\sjk.mdf;integrated Security=True;User Instance=false"))
{
conn.Open();//打开数据库
using (SqlCommand cmd =conn.CreateCommand()) //执行SQL语句
{
cmd.CommandText = "update 用户 set 登录错误次数 = 登录错误次数 + 1 where 帐号 = @zh"; //SQL语句 将用户输入的帐号对应的错误次数+1
cmd.Parameters.Add(new SqlParameter("zh", txt帐号.Text));
cmd.ExecuteNonQuery();//执行SQL语句
}
}
}
private void 错误次数清零() //错误次数清零
{
using (SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDBFilename=|DataDirectory|\sjk.mdf;integrated Security=True;User Instance=false"))
{
conn.Open();//打开数据库
using (SqlCommand cmd = conn.CreateCommand())//执行SQL语句
{
cmd.CommandText = "update 用户 set 登录错误次数 = 0 where 帐号=@zh";//SQL语句 将用户输入的帐号对应的错误次数清零。
cmd.Parameters.Add(new SqlParameter("zh", txt帐号.Text));
cmd.ExecuteNonQuery();//执行SQL语句
}
}
}
private void 禁止时间() //用户禁止时间方法
{
using (SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDBFilename=|DataDirectory|\sjk.mdf;integrated Security=True;User Instance=false"))
{
conn.Open();//打开数据库
using (SqlCommand cmd = conn.CreateCommand())//执行SQL语句
{
cmd.CommandText = "update 用户 set 恢复时间= DATEADD(mi,3,GETDATE()) where 帐号=@zh"; //SQL语句 将用户输入的帐号对应的 恢复时间设置为:当前时间+3分钟。
cmd.Parameters.Add(new SqlParameter("zh", txt帐号.Text));
cmd.ExecuteNonQuery();//执行SQL语句
}
}
}
private void button1_Click(object sender, EventArgs e) //登录按钮单击事件
{
using (SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDBFilename=|DataDirectory|\sjk.mdf;integrated Security=True;User Instance=false"))
{
conn.Open();//打开数据库
using (SqlCommand cmd =conn.CreateCommand())
{
cmd.CommandText = "select * from 用户 where 帐号=@zh";//搜索用户输入帐号。
cmd.Parameters.Add(new SqlParameter("zh", txt帐号.Text));
using (SqlDataReader reader = cmd.ExecuteReader())//将搜索结果存储到reader 中。
{
if (reader.Read())//判断reader 中是否有数据。
{
DateTime d = reader.GetDateTime(reader.GetOrdinal("恢复时间"));// 如果有数据,则获取 恢复时间 赋值给d。
if (d > DateTime.Now)//判断 获取的d 时间是否大于当前时间。
{
int i = reader.GetInt32(reader.GetOrdinal("登录错误次数"));//如果大于当前时间,则获取 错误次数。
if (i > 3)//判断错误次数是否大于3
{
MessageBox.Show("登录错误次数过多," + d + "后重试!");//如果大于3 则提示。
return;//退出。
}
}
else//如果获取的d时间小于当前时间
{
错误次数清零();//执行错误次数清零 方法。
}
string 获取数据库密码 = reader.GetString(reader.GetOrdinal("密码"));//获取密码
if (获取数据库密码 == txt密码.Text) //判断密码
{
MessageBox.Show("登录成功!");//密码正确,提示
错误次数清零();//错误次数清零。
}
else
{
MessageBox.Show("密码错误!");//密码不正确 提示
错误次数增加();//错误次数增加
禁止时间();//调用 禁止时间方法。 也可以在这里判断输入次数是否大于或等于3.在调用方法。那么上面也就无需判断 错误次数是否大于3.
}
}
else
{
MessageBox.Show("帐号错误!");//帐号错误提示。
}
}
}
}
}
}
}输出结果:
