本帖最后由 张振 于 2013-4-7 00:32 编辑
先上代码,视频案例登陆的最后锁定的解决方案
因为看不到老师最后的演示
而我自己根据老师写的代码在运行一遍发现重置错误次数为0的方法执行不了
因为前面几次错误应该把错误次数修改到了4 每次运行都直接进入锁定状态
除非用户名输入错误才能不进入锁定 所以 重置错误次数为0的想法压根就没实现
也许是有些东西我还没琢磨透 所以请教下 怎么能重置
namespace 登陆练习1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void RaseErrorTimes()
{
/** using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MyDB.mdf;Integrated Security=True;User Instance=True"))
{
conn.Open();**/这些是老师给的代码 就不用看了
using (SqlCommand updateCmd = conn.CreateCommand())
{
updateCmd.CommandText = "update T_Users Set ErrorTimes=ErrorTimes+1 where UserName=@USN";
updateCmd.Parameters.Add(new SqlParameter("USN", txtUsername.Text));
updateCmd.ExecuteNonQuery();
}
}
}
private void RasetErrorTimes()
{
using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MyDB.mdf;Integrated Security=True;User Instance=True"))
{
conn.Open();
using (SqlCommand updateCmd = conn.CreateCommand())
{
updateCmd.CommandText = "update T_Users Set ErrorTimes=0 where UserName=@USN";
updateCmd.Parameters.Add(new SqlParameter("USN", txtUsername.Text));
updateCmd.ExecuteNonQuery();
}
}
}
private void btn1_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MyDB.mdf;Integrated Security=True;User Instance=True"))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "select * from T_Users where UserName=@USN";
cmd.Parameters.Add(new SqlParameter("USN", 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("登陆成功");
RasetErrorTimes();
}
else
{
RaseErrorTimes();
MessageBox.Show("登录失败");
}
}
else
{
MessageBox.Show("用户名不存在");
return;
}
}
}
}
}
}
}
|