1. 程式人生 > >Net學習日記_ADO.Net_2_練習(登錄邏輯)

Net學習日記_ADO.Net_2_練習(登錄邏輯)

pre eno .cn lpar .com turn getdate col 日記

要求:

技術分享

頁面:

技術分享

數據庫設定

技術分享

技術分享

主程序

using System;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace Test01
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void BtOK_Click(object sender, EventArgs e)
        {
            SqlConnection conn 
= new SqlConnection("server=PC201609230944\\SQL2005;database=HeiMaBlog;user=sa;pwd=123456"); SqlCommand cmd = new SqlCommand("select *,datediff(minute,LastErrTime,GETDATE()) from UserInfo where UserName=@UserName", conn); cmd.Parameters.Add(new SqlParameter("@UserName", TxName.Text.Trim())); conn.Open(); SqlDataReader dr
= cmd.ExecuteReader(); if (dr.HasRows) { dr.Read();////不管dr取到多少條數據,只取出前面第一行(調用一次read,就會讀取一行,也只讀取一行) if (dr.GetInt32(5) < 15) { MessageBox.Show("時間未到,請稍後重試!!"); return; }
if (dr[2].ToString() == TxPwd.Text.Trim()) { MessageBox.Show("登錄成功!!"); UserInfo.ReSetErrTimes(dr.GetInt32(0));//登錄成功後,就重置錯誤時間 } else { //如果密碼不正確!! MessageBox.Show("登錄失敗,密碼錯誤"); UserInfo.UpDateErrTimes(dr.GetInt32(0)); // 錯誤三次就更新錯誤時間 if (dr.GetInt32(3) + 1 == 3) { UserInfo.UpDateLastErrTime(dr.GetInt32(0)); UserInfo.ReSetErrTimes(dr.GetInt32(0)); } } } else { MessageBox.Show("該用戶不存在!!!"); } } } }

輔助

using System.Data.SqlClient;

namespace Test01
{
    class UserInfo
    {
        /// <summary>
        /// 更新錯誤時間!!
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static int UpDateErrTimes(int id)
        {
            using (SqlConnection conn = new SqlConnection("server=PC201609230944\\SQL2005;database=HeiMaBlog;user=sa;pwd=123456"))
            {
                using (SqlCommand cmd = new SqlCommand("update UserInfo set ErrTimes=ErrTimes+1 where id=" + id, conn))
                {
                    conn.Open();
                    return cmd.ExecuteNonQuery();
                }
            }
        }

        /// <summary>
        /// 重置錯誤時間
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static int ReSetErrTimes(int id)
        {
            using (SqlConnection conn = new SqlConnection("server=PC201609230944\\SQL2005;database=HeiMaBlog;user=sa;pwd=123456"))
            {
                using (SqlCommand cmd = new SqlCommand("update UserInfo set ErrTimes=0 where id=" + id, conn))
                {
                    conn.Open();
                    return cmd.ExecuteNonQuery();
                }
            }
        }

        /// <summary>
        /// 更新3次錯誤的輸入時間
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static int UpDateLastErrTime(int id)
        {
            using (SqlConnection conn = new SqlConnection("server=PC201609230944\\SQL2005;database=HeiMaBlog;user=sa;pwd=123456"))
            {
                using (SqlCommand cmd = new SqlCommand("update UserInfo set LastErrTime = getDate() where id= " + id, conn))
                {
                    conn.Open();
                    return cmd.ExecuteNonQuery();
                }

            }

        }

    }
}

Net學習日記_ADO.Net_2_練習(登錄邏輯)