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


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



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值