using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApplication3
{
class Program
{
static string sqlstr = "Server=.;DataBase=MLERP;uid=sa;pwd=123";
static void Main(string[] args)
{
Console.WriteLine("===开始登录====");
Console.WriteLine("请输入用户名");
string userName = Console.ReadLine();
Console.WriteLine("请输入密码");
string userPass = Console.ReadLine();
using (SqlConnection conn = new SqlConnection(sqlstr))
{
conn.Open();
using (SqlCommand com = conn.CreateCommand())
{
com.CommandText = "select * from Users where userName=@userName";
com.Parameters.AddWithValue("userName", userName);
using (SqlDataReader reader = com.ExecuteReader())
{
if (reader.Read())
{
//用户存在,判断密码是否和数据库中密码一致
string db_password = reader.GetString(reader.GetOrdinal("userPass"));
int db_errortimes = reader.GetInt32(reader.GetOrdinal("errorTimes"));
DateTime hours = reader.GetDateTime(reader.GetOrdinal("errordate"));
DateTime hours1 = DateTime.Now;
TimeSpan hour1 =hours1 - hours;
if (db_errortimes > 1 && hour1.TotalMinutes < 1)
{
Console.WriteLine("错误三次,5分钟后有一次重试机会");
Console.ReadKey();
return;
}
else
{
if (db_password == userPass)
{
Console.WriteLine("登录成功");
Zerotimes(userName);
}
else
{
Console.WriteLine("密码错误");
times(userName, hours1);
}
}
}
else
{
Console.WriteLine("没有这个用户!");//没有这个用户名
}
}
}
}
}
public static void Zerotimes(string userName)//归0
{
using (SqlConnection conn = new SqlConnection(sqlstr))
{
conn.Open();
using (SqlCommand zeroCmd = conn.CreateCommand())
{
zeroCmd.CommandText = "update Users set errorTimes=0 where userName=@userName";
zeroCmd.Parameters.Add(new SqlParameter("userName", userName));
zeroCmd.ExecuteNonQuery();
}
}
}
public static void times(string userName ,DateTime time)//错误 +1
{
using (SqlConnection conn = new SqlConnection(sqlstr))
{
conn.Open();
using (SqlCommand incCmd = conn.CreateCommand())
{
incCmd.CommandText = "update Users set errorTimes=errorTimes+1 ,errorDate=@errorDate where userName=@userName";
incCmd.Parameters.Add(new SqlParameter("errorDate", time));
incCmd.Parameters.Add(new SqlParameter("userName", userName));
incCmd.ExecuteNonQuery();
}
}
}
}
}