前台代码:
<asp:Label Text="姓名:" runat="server" ID="label1"></asp:Label>
<asp:TextBox ID="txtUserName" runat="server" Text=""></asp:TextBox><br />
<asp:Label ID="label2" Text="密 码" runat="server"></asp:Label>
<asp:TextBox ID="txtPwd" Text="" runat="server"></asp:TextBox><br />
<asp:Button ID="btn" runat="server" Text="登录" OnClick="btn_Click" />
<asp:Label ID="Label3" runat="server"></asp:Label>
后台代码:
protected void btn_Click(object sender, EventArgs e)
{
string sqlread = "select * from T_user where FuseName=@name1";
SqlDataReader reader = SqlHelp.ExecuteDataReader(sqlread, new SqlParameter("name1", txtUserName.Text.Trim()));
if (reader.Read())
{
object times = reader.GetValue(2);
if ((int)times <3)
{
dengLv();
}
else if ((int)times == 3)
{
btn.Enabled = false;
string sqlWrite = "update T_user set FlastErrorDateTime=@writerTime where FuseName=@name2";
SqlHelp.ExectueNonQuery(sqlWrite, new SqlParameter("name2", txtUserName.Text.Trim()), new SqlParameter("writerTime", DateTime.Now));
}
else
{
btn.Enabled = false;
string outTimes = "select * from T_user where FuseName=@name3";
SqlDataReader reader1 = SqlHelp.ExecuteDataReader(outTimes, new SqlParameter("name3", txtUserName.Text.Trim()));
if (reader1.Read())
{
DateTime datetime =(DateTime) reader1.GetValue(3);
//C#日期相减的的方法
//DateTime t1 = DateTime.Parse("2005-01-01");
//DateTime t2 = DateTime.Parse("2006-01-01");
//System.TimeSpan ts = t2 - t1;
//int days = ts.Days;
System.TimeSpan ts = DateTime.Now - datetime;
int miniute = ts.Minutes;
if (miniute >= 15)
{
btn.Enabled = true;
string sql2 = "update T_user set FErrorTimes=0 where FuseName=@useName";
SqlHelp.ExectueNonQuery(sql2, new SqlParameter("useName", txtUserName.Text.Trim()));
}
}
}
}
}
protected void dengLv()
{
#region 登录不成功累加次数,登录成功后清零
string sql = "select * from T_user where FuseName=@name";
SqlDataReader read = SqlHelp.ExecuteDataReader(sql, new SqlParameter("name", txtUserName.Text.Trim()), new SqlParameter("pwd", txtPwd.Text.Trim()));
if (read.Read())
{
Response.Write("登录成功!!!!");
string sql2 = "update T_user set FErrorTimes=0 where FuseName=@useName";
SqlHelp.ExectueNonQuery(sql2, new SqlParameter("useName", txtUserName.Text.Trim()));
}
else
{
string sql1 = "update T_user set FErrorTimes+=1 where FuseName=@useName1";
SqlHelp.ExectueNonQuery(sql1, new SqlParameter("useName1", txtUserName.Text.Trim()));
}
read.Close();
read.Dispose();
#endregion
}
<asp:Label Text="姓名:" runat="server" ID="label1"></asp:Label>
<asp:TextBox ID="txtUserName" runat="server" Text=""></asp:TextBox><br />
<asp:Label ID="label2" Text="密 码" runat="server"></asp:Label>
<asp:TextBox ID="txtPwd" Text="" runat="server"></asp:TextBox><br />
<asp:Button ID="btn" runat="server" Text="登录" OnClick="btn_Click" />
<asp:Label ID="Label3" runat="server"></asp:Label>
后台代码:
protected void btn_Click(object sender, EventArgs e)
{
string sqlread = "select * from T_user where FuseName=@name1";
SqlDataReader reader = SqlHelp.ExecuteDataReader(sqlread, new SqlParameter("name1", txtUserName.Text.Trim()));
if (reader.Read())
{
object times = reader.GetValue(2);
if ((int)times <3)
{
dengLv();
}
else if ((int)times == 3)
{
btn.Enabled = false;
string sqlWrite = "update T_user set FlastErrorDateTime=@writerTime where FuseName=@name2";
SqlHelp.ExectueNonQuery(sqlWrite, new SqlParameter("name2", txtUserName.Text.Trim()), new SqlParameter("writerTime", DateTime.Now));
}
else
{
btn.Enabled = false;
string outTimes = "select * from T_user where FuseName=@name3";
SqlDataReader reader1 = SqlHelp.ExecuteDataReader(outTimes, new SqlParameter("name3", txtUserName.Text.Trim()));
if (reader1.Read())
{
DateTime datetime =(DateTime) reader1.GetValue(3);
//C#日期相减的的方法
//DateTime t1 = DateTime.Parse("2005-01-01");
//DateTime t2 = DateTime.Parse("2006-01-01");
//System.TimeSpan ts = t2 - t1;
//int days = ts.Days;
System.TimeSpan ts = DateTime.Now - datetime;
int miniute = ts.Minutes;
if (miniute >= 15)
{
btn.Enabled = true;
string sql2 = "update T_user set FErrorTimes=0 where FuseName=@useName";
SqlHelp.ExectueNonQuery(sql2, new SqlParameter("useName", txtUserName.Text.Trim()));
}
}
}
}
}
protected void dengLv()
{
#region 登录不成功累加次数,登录成功后清零
string sql = "select * from T_user where FuseName=@name";
SqlDataReader read = SqlHelp.ExecuteDataReader(sql, new SqlParameter("name", txtUserName.Text.Trim()), new SqlParameter("pwd", txtPwd.Text.Trim()));
if (read.Read())
{
Response.Write("登录成功!!!!");
string sql2 = "update T_user set FErrorTimes=0 where FuseName=@useName";
SqlHelp.ExectueNonQuery(sql2, new SqlParameter("useName", txtUserName.Text.Trim()));
}
else
{
string sql1 = "update T_user set FErrorTimes+=1 where FuseName=@useName1";
SqlHelp.ExectueNonQuery(sql1, new SqlParameter("useName1", txtUserName.Text.Trim()));
}
read.Close();
read.Dispose();
#endregion
}