在原来数据库上加上次数统计列ErrorTimes
源码记录
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.Data.SqlClient;
namespace 登录厕所里hi
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void incErrorTimes()
{
using (SqlConnection conn = new
SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\MyDb1.mdf;Integrated Security=True;User Instance=True"))
{
conn.Open();
using (SqlCommand updatecmd = conn.CreateCommand())
{
updatecmd.CommandText = "update T_Users Set ErrorTimes+=1 where UserName =@UserName";
updatecmd.Parameters.Add(new SqlParameter("UserName", txtUserName.Text));
updatecmd.ExecuteNonQuery();
}
}
}
private void button1_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new
SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\MyDb1.mdf;Integrated Security=True;User Instance=True"))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "select * from T_Users where UserName=@UserName";
cmd.Parameters.Add(new SqlParameter("UserName", txtUserName.Text));
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())
{
int errorTimes = reader.GetInt32(reader.GetOrdinal("ErrorTimes"));
if(errorTimes >3)
{
MessageBox.Show("登录次数过多");
return ;
}
string dbpassword =reader.GetString(reader.GetOrdinal("Password"));
if(dbpassword==txtPassword.Text)
{
MessageBox.Show("登录成功");
}
else
{
incErrorTimes();
MessageBox.Show("登录失败");
}
}
else
{
MessageBox.Show("用户名不存在");
}
}
}
}
}
}
}
本文介绍了一个简单的登录系统中如何通过数据库记录用户的登录错误次数,并在错误次数超过设定阈值时阻止用户继续尝试登录。
360

被折叠的 条评论
为什么被折叠?



