---------------------- 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/