forked from kenve/PAMSystem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOperatorDAL.cs
More file actions
111 lines (109 loc) · 3.92 KB
/
Copy pathOperatorDAL.cs
File metadata and controls
111 lines (109 loc) · 3.92 KB
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
using PAMSystem.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PAMSystem.DAL
{
public class OperatorDAL
{
//根据Id获得数据
public Operator GetById(Guid id)
{
DataTable table = SqlHelper.ExecuteDataTable("select * from T_Operator where Id=@Id and IsDeleted=0",
new SqlParameter("@Id", id));
if (table.Rows.Count <= 0)
{
return null;
}
else if (table.Rows.Count > 1)
{
throw new Exception("存在Id重复用户!");
}
else
{
return ToOperator(table.Rows[0]);
}
}
//通过用户名查询
public Operator GetByUserName(string userName)
{
DataTable table = SqlHelper.ExecuteDataTable(@"select * from T_Operator where UserName=@UserName and IsDeleted=0",
new SqlParameter("@UserName", userName));
if (table.Rows.Count <= 0)
{
return null;
}
else if (table.Rows.Count > 1)
{
throw new Exception("用户名重复了!");
}
else
{
return ToOperator(table.Rows[0]);
}
}
//根据Id删除数据用户
public int DeleteById(Guid id)
{
//软删除
return SqlHelper.ExecuteNonQuery("Update T_Operator Set IsDeleted=1 where Id=@Id",
new SqlParameter("@Id", id));
}
//恢复已删除用户
public int ReuseOperator(Guid id)
{
return SqlHelper.ExecuteNonQuery("Update T_Operator Set IsDeleted=0 where Id=@Id",
new SqlParameter("@Id", id));
}
//显示所有用户信息
public Operator[] ListAll()
{
DataTable dt = SqlHelper.ExecuteDataTable("select * from T_Operator where IsDeleted=0");
Operator[] operators = new Operator[dt.Rows.Count];
for (int i = 0; i < dt.Rows.Count; i++)
{
operators[i] = ToOperator(dt.Rows[i]);
}
return operators;
}
//执行新增(插入)数据,或判断密码是否正确
public int Insert(Operator operators)
{
return SqlHelper.ExecuteNonQuery(@"insert into T_Operator(
Id,UserName,Password,IsDeleted,IsLocked) values(newid(),@UserName,@Password,0,0)",
new SqlParameter("@UserName", operators.UserName),
new SqlParameter("@Password", operators.Password));
}
//赋值给Operator属性值
private Operator ToOperator(DataRow row)
{
Operator operators = new Operator();
operators.Id = (Guid)row["Id"];
operators.UserName = (string)row["UserName"];
operators.Password = (string)row["Password"];
operators.IsDeleted = (bool)row["IsDeleted"];
operators.IsLocked = (bool)row["IsLocked"];
return operators;
}
//修改用户信息,更改用户密码,不改用户名
public int Update(Guid id, string pwd)
{
int i=SqlHelper.ExecuteNonQuery("update T_Operator set Password=@Password where Id=@Id",
new SqlParameter("@Password", pwd),
new SqlParameter("@Id", id));
return i;
}
//修改用户信息,并且修改密码
public int Update(Guid id, string userName,string pwd)
{
return SqlHelper.ExecuteNonQuery("update T_Operator set UserName=@UserName,Password=@Password where Id=@Id",
new SqlParameter("@UserName", userName),
new SqlParameter("@Password", pwd),
new SqlParameter("@Id", id));
}
}
}