您的位置:首页 > 其它

登录程序(输入错误十五秒后才能再次输入)

2012-02-22 10:01 204 查看
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 whereUserName=@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);

}

}//获取时间间隔

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐