改进用户登录,实现
1.控制用户登录的错误次数,超过三次不允许继续登录
2.用户连续登陆错误次数达到3次后,2分钟之内不允许继续登录,2分钟之后才允许继续登录
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;
using System.Configuration;
namespace 用户登录NO._1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button_Update() //这里是用户每次登录的时候先更新当前用户的Error字符。这样就实现了实时可靠
{
string strcon = ConfigurationManager.ConnectionStrings["con_1"].ConnectionString; .//这里是取链接字符串
using (SqlConnection con = new SqlConnection(strcon))
{
using (SqlCommand cmd = new SqlCommand("select * from T_user whereusername=@name", con))
{
con.Open();
cmd.Parameters.AddWithValue("@name",txtName.Text.Trim());
SqlDataReader myreader = cmd.ExecuteReader();
while (myreader.Read())
{
if (myreader["ErrorTime"] != System.DBNull.Value) //判断时间字段是否为空
{
if ((System.DateTime.Now - Convert.ToDateTime(myreader["ErrorTime"].ToString())).TotalMinutes > 2) //这里可以用截子串的方法进行操作,我这里 C#中的TotalMinutes来写的的
//public double TotalMilliseconds { get; }
//
///分钟的小数部分表示的当前 System.TimeSpan 结构的值。
//
// 返回结果:
// 此实例表示的总分钟数。
{
Update_User(myreader["UserName"].ToString()); //这里调用的是更新方法
}
}
}
}
}
}
/// <summary>
/// 更新所有数据
/// </summary>
/// <param name="name"></param>
private void Update_User(string name) //更新当前用户的方法
{
string strcon = ConfigurationManager.ConnectionStrings["con_1"].ConnectionString;
using (SqlConnection con = new SqlConnection(strcon))
{
using (SqlCommand cmd1 = new SqlCommand("update T_user set Error=0 whereusername=@name", con))
{
con.Open();
cmd1.Parameters.AddWithValue("@name", name);
cmd1.ExecuteNonQuery();
}
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
int i = 0; //这个全局变量也可以做一个累加器,但是会很没意义我把用i定义的方法删除了
private void button1_Click(object sender, EventArgs e)
{
Button_Update(); //更新数据
string strcon = ConfigurationManager.ConnectionStrings["con_1"].ConnectionString;
SqlConnection con = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand("select * from T_user where username=@name and password=@pwd",con);
cmd.Parameters.AddWithValue("@name",txtName.Text.Trim());
cmd.Parameters.AddWithValue("@pwd",txtpwd.Text.Trim());
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
if(dt.Rows.Count==1) //密码正确的情况
{
if (dt.Rows[0]["Error"].ToString() == "3") //从数据库中取出其2分钟内错误的个数量
{
MessageBox.Show("请稍等……"); //这里是不许其操作的
}
else
{
MessageBox.Show("登录成功!");
}
}else //这里是用户名或密码不正确的的情况
{
NewMethod();
}
}
private void NewMethod()
{
string strcon = ConfigurationManager.ConnectionStrings["con_1"].ConnectionString;
using (SqlConnection con = new SqlConnection(strcon))
{
using (SqlCommand cmd = new SqlCommand("select * from T_User whereusername=@name", con))
{
con.Open();
cmd.Parameters.Add("@name", txtName.Text.Trim());
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count > 0)
{
if (dt.Rows[0]["Error"].ToString() != "3")
{
//用i不合适不容易控制
//if (i == 3) //此为I的方法(这是没必要的)
//{
// method_sqlhelper(i);
// MessageBox.Show("您已经错误3次请2分钟之后再进行登录!");
//}
//else
//{
MessageBox.Show("登录失败!");
method_sqlhelper(i);
//}
}
else
{
MessageBox.Show("请稍后……");
}
}
}
}
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void method_sqlhelper(int i)
{
string str_con = ConfigurationManager.ConnectionStrings["con_1"].ConnectionString;
SqlConnection con = new SqlConnection(str_con);
SqlCommand cmd = new SqlCommand("update T_User set Error=Error+1,ErrorTime=@time whereusername=@name",con);
cmd.Parameters.AddWithValue("@name",txtName.Text.Trim());
cmd.Parameters.AddWithValue("@time", System.DateTime.Now);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
在写这一篇文件的时候,我想起了一个更好的办法,不用这么频繁的操作数据库,同时呢在我写第一行注释的时候,我发现其中还有一个小小的逻辑问题,不过当然不会影响其正确的执行,会影响一点其执行效率!