-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserLogin.gitattributes
125 lines (115 loc) · 4.7 KB
/
UserLogin.gitattributes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using System;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;
namespace UserLogin
{
class Verifiction
{
/// <summary>
/// 数据库连接以及判断
/// </summary>
/// <param name="UsId">用户名</param>
/// <param name="Pwd">登录密码</param>
/// <returns></returns>
public int DatabaseConn(string UsId,string Pwd)
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["sql1"].ConnectionString))
{
conn.Open();
using (SqlCommand comm = new SqlCommand())
{
UserInfo userInfo = null;
comm.CommandTimeout = 4;
comm.Connection = conn;
comm.CommandType = CommandType.Text;
comm.CommandText = "select * from UserLogin where UsAccount=@UsAcount";
comm.Parameters.AddWithValue("@UsAcount", UsId);
comm.Parameters.AddWithValue("@UsPassWord", Pwd);
using (SqlDataReader datareader = comm.ExecuteReader())
{
if(datareader.Read())
{
userInfo = new UserInfo
{
UserId = Convert.ToInt16(datareader["UsId"]),
UserName = datareader["UsAccount"].ToString(),
UserPwd = datareader["UsPassWord"].ToString(),
ErrorTime = Convert.ToInt16(datareader["UsErrorTime"]),
LastDataTime = Convert.ToDateTime(datareader["UsLastDateTime"])
};
}
}
if(userInfo != null)
{
if (userInfo.ErrorTime >= 3)
{
if (DateTime.Now.Subtract(userInfo.LastDataTime).Seconds > 15)
{
InitialErrorTimeAndDate(comm, userInfo);
}
else
{
return 2;
}
}
if (userInfo.UserPwd != Pwd)
{
AddErrorTimeAndDate(comm, userInfo);
return 1;
}
else
{
return 0;
}
}
else
{
return 1;
}
}
}
}
/// <summary>
/// 初始化时错误次数
/// </summary>
/// <param name="comm">执行语句</param>
/// <param name="userInfo">用户属性类</param>
private void InitialErrorTimeAndDate(SqlCommand comm, UserInfo userInfo)
{
comm.CommandText = "update UserLogin set UsErrorTime=0,UsLastDateTime='1960-1-1 00:00:00.000' where UsId=@UsId";
comm.Parameters.AddWithValue("@UsId", userInfo.UserId);
comm.ExecuteNonQuery();
}
/// <summary>
/// 增加错误次数和时间
/// </summary>
/// <param name="comm">执行语句</param>
/// <param name="userInfo">用户属性类</param>
private void AddErrorTimeAndDate(SqlCommand comm,UserInfo userInfo)
{
comm.CommandText = "update UserLogin set UsErrorTime=@UsErrorTime,UsLastDateTime=getdate() where UsId=@UsId";
comm.Parameters.AddWithValue("@UsErrorTime", userInfo.ErrorTime + 1);
comm.Parameters.AddWithValue("@UsId", userInfo.UserId);
comm.ExecuteNonQuery();
comm.CommandText = "insert into ErrorDateils(ErrorDateTime) values (getDate())";
comm.ExecuteNonQuery();
}
/// <summary>
/// 登录结果判定工厂
/// </summary>
/// <param name="UsId">用户账号</param>
/// <param name="UsPassWord">用户密码</param>
public void LoginStateFactory(string UsId,string UsPassWord)
{
int LoginStateCode= DatabaseConn(UsId, UsPassWord);
switch(LoginStateCode)
{
case 0:MessageBox.Show("登录成功"); break;
case 1:MessageBox.Show("账号或密码错误"); break ;
case 2:MessageBox.Show("账号已经被锁定"); break;
}
}
}
}