您的位置:首页 > 职场人生

黑马程序员-ADO.NET登录页面中的错误次数过多禁止登陆

2012-08-18 10:32 120 查看
---------------------- Windows Phone 7手机开发.Net培训、期待与您交流! ----------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 登录错误次数winform
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
///  重设错误次数的方法
/// </summary>
/// <param name="users">数据库中的哪个用户</param>
private void ResetErrorTimes(string users)
{
using (SqlConnection conn = new SqlConnection(@"Data Source=.\;Database=test;user=sa;password=123456"))
{
conn.Open();
using (SqlCommand resetErrorTimesCmd = conn.CreateCommand())
{
resetErrorTimesCmd.CommandText = "update t_winusers set errortimes=0 where username=@u";
resetErrorTimesCmd.Parameters.Add(new SqlParameter("u", users));
resetErrorTimesCmd.ExecuteNonQuery();
}
}

}
/// <summary>
/// 增加错误次数的方法
/// </summary>
private void SetErrorTimes()
{
using (SqlConnection conn = new SqlConnection(@"Data Source=.\;Database=test;user=sa;password=123456"))
{
conn.Open();
using (SqlCommand SetErrorTimesCmd = conn.CreateCommand())
{
SetErrorTimesCmd.CommandText = "update t_winusers set [errortimes]=[errortimes]+1 where username=@username";
SetErrorTimesCmd.Parameters.Add(new SqlParameter("username", userTextBox.Text));
SetErrorTimesCmd.ExecuteNonQuery();
}

}
}
private void loginbutton_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(@"Data Source=.\;Database=test;user=sa;password=123456"))
{
conn.Open();
using (SqlCommand loginCmd = conn.CreateCommand())
{
loginCmd.CommandText = "SELECT [username],[password],[errortimes] FROM [test].[dbo].[t_winusers] where username=@username";

//使用参数化查询可有效防止SQL注入漏洞
loginCmd.Parameters.Add(new SqlParameter("username", userTextBox.Text));
using (SqlDataReader reader = loginCmd.ExecuteReader())
{
if (reader.Read())//如果结果集中有记录read()返回trun,直到遍历到最后一条记录后,返回false//有记录说明用户名正确
{

if (reader.GetInt32(reader.GetOrdinal("errortimes")) < 3)  //用户名正确,则读取出结果集中errortimes字段的值,判断错误次数是否小于三次
{
if (passwordTextBox.Text == reader.GetString(reader.GetOrdinal("password")))
{
MessageBox.Show("登陆成功");
ResetErrorTimes(reader.GetString(reader.GetOrdinal("username")));
return;
}
else
{
MessageBox.Show("密码错误");
SetErrorTimes();

}
}
else
{
MessageBox.Show("错误次数过多,禁止登录!!");
return;

}
}
else
{
MessageBox.Show("用户名不存在");
}
}
}
}
}

private void resetButton_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(@"Data Source=.\;Database=test;user=sa;password=830916"))
{
conn.Open();
using (SqlCommand loginCmd = conn.CreateCommand())
{
loginCmd.CommandText = "SELECT [username],[password],[errortimes] FROM [test].[dbo].[t_winusers] where username=@username";
loginCmd.Parameters.Add(new SqlParameter("username", userTextBox.Text));
using (SqlDataReader reader = loginCmd.ExecuteReader())
{
if (reader.Read())
{
ResetErrorTimes(userTextBox.Text);
}
else
{
MessageBox.Show("用户名不存在");
}
}
}
}
}
}
}


---------------------- Windows Phone 7手机开发.Net培训、期待与您交流! ----------------------详细请查看:http://net.itheima.com/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: