Repository navigation
Expand file tree
/
Copy pathIdNameDAL.cs
More file actions
87 lines (84 loc) · 2.64 KB
/
Copy pathIdNameDAL.cs
File metadata and controls
87 lines (84 loc) · 2.64 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
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 IdNameDAL
{
//根据Category获得IdName的数组
public IdName[] GetByCategory(string category)
{
DataTable table = SqlHelper.ExecuteDataTable("select Id,Name from T_IdName where Category=@Category",
new SqlParameter("@Category", category));
IdName[] items = new IdName[table.Rows.Count];
for (int i = 0; i < table.Rows.Count; i++)
{
DataRow row = table.Rows[i];
IdName idname = new IdName();
idname.Id = (Guid)row["Id"];
idname.Name = (string)row["Name"];
items[i] = idname;
}
return items;
}
public IdName GetById(Guid id)
{
DataTable table = SqlHelper.ExecuteDataTable("select * from T_IdName where Id=@Id",
new SqlParameter("@Id", id));
if (table.Rows.Count <= 0)
{
return null;
}
else if (table.Rows.Count > 1)
{
throw new Exception("存在Id重复用户!");
}
else
{
return ToIdName(table.Rows[0]);
}
}
private IdName ToIdName(DataRow row)
{
IdName idname= new IdName();
idname.Id = (Guid)row["Id"];
idname.Name = (string)row["Name"];
idname.Category = (string)row["Category"];
return idname;
}
//显示所有
public IdName[] ListAll()
{
DataTable table = SqlHelper.ExecuteDataTable("select * from T_IdName");
IdName[] idname = new IdName[table.Rows.Count];
for (int i = 0; i < table.Rows.Count; i++)
{
idname[i] = ToIdName(table.Rows[i]);
}
return idname;
}
//通过Name查找
public IdName GetByName(string name)
{
DataTable table = SqlHelper.ExecuteDataTable("select * from T_IdName where Name=@Name",
new SqlParameter("@Name", name));
if (table.Rows.Count <= 0)
{
return null;
}
else if (table.Rows.Count > 1)
{
throw new Exception("存在Id重复用户!");
}
else
{
return ToIdName(table.Rows[0]);
}
}
}
}