从mysql数据库判断验证登陆信息_数据库查询写法:从登陆验证得到一些启示和经验...

这几天做了一个登陆验证的案例,主要功能就是:用户输入密码进行登陆,如果输错密码三次则15分钟内不能登陆。

这个案例确实不复杂,但是其中的逻辑第一次理解确实很别扭。

从这个小案例中,我得到了几个启示,并打算以后写类似的程序都执行此标准

1.该项目中,和sql有关的操作统统放到SqlHelper中 ---所有相同的操作都应该单独定义成一个助手类。

2.该项目中,使用了参数化查询有效防止了sql注入漏洞 ---所有关于查询的并且值由用户输入的sql语句,一律不准用字符串拼接

3.该项目中,判断用户是否存在的办法是使用SqlDataReader读取数据,然后判断dr.HasRows --- 这样做简便了查询,而且将整行的值取出,其他数据可以被另外代码使用

从这个项目中,我学到了这几点新知识

1.比较数据库中存储的时间和当前的时间相差多少分钟、多少小时、多少天……都可以使用dateDiff函数。而不需要将它取出来用.NET的DateTime类来处理(利用TimeSpan=DateTime-DateTime)

2.用户密码输错三次15分钟后的业务逻辑

窗体文件:

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Data;usingSystem.Data.SqlClient;namespace密码输错三次15分钟后重试

{public partial classForm1 : Form

{publicForm1()

{

InitializeComponent();

}private void btnLogin_Click(objectsender, EventArgs e)

{if(String.IsNullOrWhiteSpace(txtUserName.Text)||String.IsNullOrWhiteSpace(txtPassWord.Text))

{

MessageBox.Show("输入有误!");return;

}using (SqlConnection conn = new SqlConnection("server=.;database=WebSite;uid=sa;pwd=123456"))

{using (SqlCommand cmd =conn.CreateCommand())

{

cmd.CommandText= "select *,datediff(minute, uLastErrTime,getdate()) from UserInfo where uName=@name";

cmd.Parameters.Add(new SqlParameter("@name", txtUserName.Text.Trim()));

conn.Open();//使用using对SqlDataReader进行资源管理

using (SqlDataReader dr =cmd.ExecuteReader())

{if(dr.HasRows)

{

dr.Read();if (dr.GetInt32(5) > 15)

{//重置uErrTimes

SqlHelper.ResetErrTimes(dr.GetInt32(0));

}//加上这句话,可以解决结果集不同步的情况//判断次数只能在时间小于15分钟的情况下才能进行

if (dr.GetInt32(5) <= 15)

{if (dr.GetInt32(3) >= 3)

{

MessageBox.Show("密码错误已三次,请在15分钟后登陆!");return;

}

}if (String.Equals(dr.GetString(2), txtPassWord.Text.Trim()))

{

MessageBox.Show("登陆成功!");

}else{

MessageBox.Show("密码错误,登陆失败");//更新错误次数

SqlHelper.UpdateErrTimes(dr.GetInt32(0));//记录错误时间

SqlHelper.RecordErrTime(dr.GetInt32(0));

}

}else{

MessageBox.Show("不存在此账户!");return;

}

}

}

}

}

}

}

SQlHelper.cs助手类

//文件:SqlHelper.cs

//作者:Me

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Data.SqlClient;usingSystem.Windows.Forms;namespace密码输错三次15分钟后重试

{///

///包含各种对数据库的操作///

classSqlHelper

{///

///sql链接字符串///

private static string sqlStr = "server=.;database=WebSite;uid=sa;pwd=123456";///

///更新错误次数///

///

public static void UpdateErrTimes(intid)

{using (SqlConnection conn = newSqlConnection(sqlStr))

{using (SqlCommand cmd =conn.CreateCommand())

{

cmd.CommandText= "update UserInfo set uErrTimes=uErrTimes+1 where uId=@id";

cmd.Parameters.Add(new SqlParameter("@id", id));try{

conn.Open();

cmd.ExecuteNonQuery();

}catch(Exception ex)

{

MessageBox.Show("数据库失败!" +ex.Message);

}

}

}

}///

///重置错误次数///

///

public static void ResetErrTimes(intid)

{using (SqlConnection conn = newSqlConnection(sqlStr))

{using (SqlCommand cmd =conn.CreateCommand())

{

cmd.CommandText= "update UserInfo set uErrTimes=0 where uId=@id";

cmd.Parameters.Add(new SqlParameter("@id", id));try{

conn.Open();

cmd.ExecuteNonQuery();

}catch(Exception ex)

{

MessageBox.Show("数据库失败!" +ex.Message);

}

}

}

}///

///记录错误时间///

///

public static void RecordErrTime(intid)

{using (SqlConnection conn = newSqlConnection(sqlStr))

{using (SqlCommand cmd =conn.CreateCommand())

{

cmd.CommandText= "update UserInfo set uLastErrTime=getdate() where uId=@id";

cmd.Parameters.Add(new SqlParameter("@id", id));try{

conn.Open();

cmd.ExecuteNonQuery();

}catch(Exception ex)

{

MessageBox.Show("数据库失败!" +ex.Message);

}

}

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值