登錄程序(輸入錯誤十五秒後才能再次輸入)

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.Data.SqlClient;
using System.Configuration;

namespace Login
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //string strcon = string.Empty;
        string strcon = ConfigurationManager.ConnectionStrings["sqlcon"].ConnectionString;
        int errorcount = 0;
        private void denglu_Click(object sender, EventArgs e)
        {
            SqlConnection sqlcnn = new SqlConnection(strcon);
            sqlcnn.Open();
            SqlCommand sqlcmm = sqlcnn.CreateCommand();
            sqlcmm.CommandText = "select * from Login where
UserName=@username andPassword=@password";
            sqlcmm.Parameters.AddWithValue("@username", TxtName.Text);
            sqlcmm.Parameters.AddWithValue("@password",TxtPassword.Text);
            SqlDataAdapter adapter = new SqlDataAdapter(sqlcmm);
            DataTable table = new DataTable();
            adapter.Fill(table);
            errorcount = GetError();
            if (errorcount>=3)
            {
                int secondspan = GetErrorTime1();
                if (secondspan<15)
                {
                    MessageBox.Show("您已經連續輸入三次錯誤密碼了,請15秒後再登錄!");
                    return;
                }
            }
            if (table.Rows.Count<=0)
            {
                errorcount++;
                UpdateError(errorcount);
                UpdateErrorTime();
                MessageBox.Show("登錄失敗!");
            }
            else if (table.Rows.Count==1)
            {
                errorcount = 0;
                UpdateError(errorcount);
                MessageBox.Show("登錄成功!");
            }
            else
            {
                MessageBox.Show("系統有重複數據!");
            }
            sqlcmm.Dispose();
            sqlcnn.Dispose();
        }
        private int GetError()
        {
            SqlConnection sqlcon = new SqlConnection(strcon);
            sqlcon.Open();
            SqlCommand sqlcmm = sqlcon.CreateCommand();
            sqlcmm.CommandText = "select Error from Login where
UserName=@username";
            sqlcmm.Parameters.AddWithValue("@username", TxtName.Text);
            object obj = sqlcmm.ExecuteScalar();
            if (DBNull.Value.Equals(obj)==true)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            } 
        }//獲取錯誤次數
       private void  UpdateError(int errorcount)
        {
            SqlConnection sqlcon = new SqlConnection(strcon);
            sqlcon.Open();
            SqlCommand sqlcmm = sqlcon.CreateCommand();
            sqlcmm.CommandText = "update Login set 
Error=@error  whereUserName=@username";
            sqlcmm.Parameters.AddWithValue("@username",TxtName.Text);
            sqlcmm.Parameters.AddWithValue("@error",errorcount);
            sqlcmm.ExecuteNonQuery();
        }//更新錯誤次數
       private void UpdateErrorTime()
       {
           SqlConnection conn = new SqlConnection(strcon);
           SqlCommand cmd = new SqlCommand();
           cmd.Connection = conn;
           conn.Open();
           cmd.CommandText = "update Login set
ErrorTime=@errortime whereUserName=@username";
           cmd.Parameters.AddWithValue("@username",TxtName.Text);
           cmd.Parameters.AddWithValue("@errortime",DateTime.Now);
           cmd.ExecuteNonQuery();
       }//更改錯誤時的時間
       private int GetErrorTime1()
       {
           SqlConnection conn = new SqlConnection(strcon);
           SqlCommand cmd = new SqlCommand();
           cmd.Connection = conn;

           conn.Open();
           cmd.CommandText = "select DATEDIFF([second],ErrorTime,getdate()) from Login where
UserName=@username";
           //DATEDIFF([second],ErrorTime,getdate()) 求時間差
           cmd.Parameters.AddWithValue("@username",this.TxtName.Text);
           object obj = cmd.ExecuteScalar();
           if (DBNull.Value.Equals(obj)==true)
           {
               return 0;
           }
           else
           {
               return Convert.ToInt32(obj);
           }
          
       }//獲取時間間隔
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章