1. 程式人生 > >使用者登入(登陸失敗3次,2分鐘後再登陸)

使用者登入(登陸失敗3次,2分鐘後再登陸)

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


namespace 作業_使用者登入
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private int maxerrortimes = 3;
        private int intervalMinute = 2;


        private void button1_Click(object sender, EventArgs e)
        {
            string strcon = ConfigurationManager.ConnectionStrings["strcon"].ConnectionString;
            SqlConnection conn = new SqlConnection(strcon);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = "select COUNT(username) from T_User where
[email protected]
";
            cmd.Parameters.Add(new SqlParameter("@username", this.txtusername.Text));
            //cmd.Parameters.Add(new SqlParameter("@password", this.txtpassword.Text));
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            if (table.Rows.Count > 0)
            {
                cmd.Connection = conn;
                cmd.CommandText = "SELECT Error,ErrorTime FROM T_User WHERE username='" + this.txtusername.Text + "'";
                SqlDataAdapter ad = new SqlDataAdapter(cmd);
                DataTable tb = new DataTable();
                adapter.Fill(tb);
                int errTimes = Convert.ToInt32(tb.Rows[0]["Error"]); //登陸失敗的次數
                if (errTimes == maxerrortimes) //已經達到允許登陸失敗的最大次數
                {
                    DateTime dtLast = Convert.ToDateTime(tb.Rows[0]["ErrorTime"]); //達到最大登陸次數的時間
                    TimeSpan ts1 = new TimeSpan(dtLast.Ticks);
                    TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks);  //當前時間
                    TimeSpan ts = ts2.Subtract(ts1).Duration();
                    if (ts.Minutes < this.intervalMinute)   //登陸間隔時間小於規定的時間
                    {
                        MessageBox.Show("請" + this.intervalMinute + "分鐘後再登陸!");
                    }
                    else   //達到間隔時間後,清0的登陸失敗次數,視其為第一次登陸
                    {
                        cmd.Connection = conn;
                        cmd.CommandText = "update T_User set errortime=getdate(),error=0 where username='" + this.txtusername.Text + "'";
                        conn.Open();
                        cmd.ExecuteNonQuery();
                        conn.Close();
                        LoginLess(0);
                    }


                }
                else   //登陸還未失敗或失敗次數小於最大次數
                {
                    LoginLess(errTimes);
                }


            }
            else
            {
                MessageBox.Show("使用者不存在");
            }
        }
        private void LoginLess(int tempErrorTimes)
        {
            string strcon = ConfigurationManager.ConnectionStrings["strcon"].ConnectionString;
            SqlConnection conn = new SqlConnection(strcon);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = "select * from T_User where
[email protected]
and [email protected]";
            cmd.Parameters.Add(new SqlParameter("@username", this.txtusername.Text));
            cmd.Parameters.Add(new SqlParameter("@password", this.txtpassword.Text));
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            if (table.Rows.Count <= 0)
            {
                if (tempErrorTimes == this.maxerrortimes - 1)  //已經登陸失敗最大次數
                {
                    cmd.Connection = conn;
                    cmd.CommandText = "update T_User set errortime=getdate(),error=3 where username='" + this.txtusername.Text + "'";
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                    MessageBox.Show("您已經連續登陸失敗" + maxerrortimes.ToString() + "次,請" + this.intervalMinute + "分鐘後再登陸!");
                }
                else   //其它登陸失敗次數
                {
                    cmd.Connection = conn;
                    cmd.CommandText = "UPDATE  T_User SET errortime = GETDATE(), Error = Error+1" + " WHERE username='" + this.txtusername.Text + "'";
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                    MessageBox.Show("您已經連續登陸失敗" + Convert.ToString(tempErrorTimes + 1) + "次,你還有" + Convert.ToString(this.maxerrortimes - tempErrorTimes - 1) + "次登陸機會");
                }
            }
            else //登陸成功,將登陸錯誤次數清0
            {
                cmd.Connection = conn;
                cmd.CommandText = "UPDATE  T_User SET Error =0" + " WHERE username='" + this.txtusername.Text + "'";
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
                MessageBox.Show("登陸成功!");
 
            }
        }
    }
}