在VS的开发环境中,可以有多种方式取得数据集,比如直接添加控件生成、在代码中灵活的使用SqlClient、或者比较麻烦的,就象我的这种取数据的方式,这种方式的优点在做小项目、一个人开发的时候有些麻烦,写的代码量最大,但如果是多人开发的大项目,就显示出了优点。
在对数据的操作过程中,我使用了自己封装的一个数据操作类,具体操作请参考微软提供的商店例子。操作时,将数据库的连接字符串放在Web.config中,根据需要你可以对connectionStrings节进行加密,这取决于你的需要。





因为功能简单,所有的方法我都放在了一个类中:
1
public class DALClass
2
{
3
public DALClass()
4
{
5
//
6
//TODO: 在此处添加构造函数逻辑
7
//
8
}
9
10
/// <summary>
11
/// 新增图书
12
/// </summary>
13
/// <param name="mc"></param>
14
public void Books_Insert(ModelClass mc)
15
{
16
SqlParameter[] sp = new SqlParameter[]{
17
new SqlParameter("@mc", SqlDbType.NVarChar, 20),
18
new SqlParameter("@cbs", SqlDbType.NVarChar, 60),
19
new SqlParameter("@dj", SqlDbType.Money),
20
new SqlParameter("@nrty", SqlDbType.NVarChar, 2000),
21
new SqlParameter("@tp", SqlDbType.Image)
22
};
23
sp[0].Value = mc.Mc;
24
sp[1].Value = mc.Cbs;
25
sp[2].Value = mc.Dj;
26
sp[3].Value = mc.Nrty;
27
sp[4].Value = mc.Tp;
28
29
SunnySoft.DBUtility35.SqlHelper.ExecuteNonQuery(SunnySoft.DBUtility35.SqlHelper.SQLConnectionString1,
30
CommandType.StoredProcedure, "Books_AddValue", sp);
31
}
32
33
/// <summary>
34
/// 修改图书
35
/// </summary>
36
/// <param name="mc"></param>
37
public void Books_Update(ModelClass mc)
38
{
39
SqlParameter[] sp = new SqlParameter[]{
40
new SqlParameter("@id", SqlDbType.Int),
41
new SqlParameter("@mc", SqlDbType.NVarChar, 20),
42
new SqlParameter("@cbs", SqlDbType.NVarChar, 60),
43
new SqlParameter("@dj", SqlDbType.Money),
44
new SqlParameter("@nrty", SqlDbType.NVarChar, 2000),
45
new SqlParameter("@tp", SqlDbType.Image)
46
};
47
sp[0].Value = mc.Id;
48
sp[1].Value = mc.Mc;
49
sp[2].Value = mc.Cbs;
50
sp[3].Value = mc.Dj;
51
sp[4].Value = mc.Nrty;
52
sp[5].Value = mc.Tp;
53
54
SunnySoft.DBUtility35.SqlHelper.ExecuteNonQuery(SunnySoft.DBUtility35.SqlHelper.SQLConnectionString1,
55
CommandType.StoredProcedure, "Books_Update", sp);
56
}
57
58
/// <summary>
59
/// 删除图书
60
/// </summary>
61
/// <param name="mc"></param>
62
public void Books_Delete(int id)
63
{
64
SqlParameter sp = new SqlParameter("@id", SqlDbType.Int);
65
sp.Value = id;
66
67
SunnySoft.DBUtility35.SqlHelper.ExecuteNonQuery(SunnySoft.DBUtility35.SqlHelper.SQLConnectionString1,
68
CommandType.StoredProcedure, "Books_Delete", sp);
69
}
70
71
/// <summary>
72
/// 取图书单条记录
73
/// </summary>
74
/// <param name="id">条件ID</param>
75
/// <returns></returns>
76
public ModelClass Books_GetValue(int id)
77
{
78
ModelClass mc = null;
79
SqlParameter sp = new SqlParameter("@id", SqlDbType.Int);
80
sp.Value = id;
81
82
using (SqlDataReader rdr = SunnySoft.DBUtility35.SqlHelper.ExecuteReader(
83
SunnySoft.DBUtility35.SqlHelper.SQLConnectionString1, CommandType.StoredProcedure,
84
"Books_GetValue", sp))
85
{
86
if (rdr.Read())
87
mc = new ModelClass(
88
rdr.GetInt32(0),
89
rdr.GetString(1),
90
rdr.IsDBNull(2) ? "" : rdr.GetString(2),
91
Convert.ToDouble(rdr.GetValue(3)),
92
rdr.IsDBNull(4) ? "" : rdr.GetString(4),
93
rdr.IsDBNull(5) ? new byte[]{} : (byte[])rdr.GetValue(5)
94
);
95
else
96
mc = new ModelClass();
97
}
98
return mc;
99
}
100
101
/// <summary>
102
/// 取图书数据
103
/// </summary>
104
/// <returns></returns>
105
public IList Books_GetValues()
106
{
107
IList list = new List<ModelClass>();
108
using (SqlDataReader rdr = SunnySoft.DBUtility35.SqlHelper.ExecuteReader(
109
SunnySoft.DBUtility35.SqlHelper.SQLConnectionString1, CommandType.StoredProcedure,
110
"Books_GetValues"))
111
{
112
while (rdr.Read())
113
{
114
ModelClass mc = new ModelClass(
115
rdr.GetInt32(0),
116
rdr.GetString(1),
117
rdr.IsDBNull(2) ? "" : rdr.GetString(2),
118
Convert.ToDouble(rdr.GetValue(3)),
119
rdr.IsDBNull(4) ? "" : rdr.GetString(4),
120
new byte[]{}
121
);
122
list.Add(mc);
123
}
124
}
125
return list;
126
}
127
128
/// <summary>
129
/// 登录人员校验
130
/// </summary>
131
/// <param name="dlmc"></param>
132
/// <returns></returns>
133
public UserClass User_GetValue(string dlmc)
134
{
135
UserClass mc = null;
136
SqlParameter sp = new SqlParameter("@dlmc", SqlDbType.NVarChar, 20);
137
sp.Value = dlmc;
138
139
using (SqlDataReader rdr = SunnySoft.DBUtility35.SqlHelper.ExecuteReader(
140
SunnySoft.DBUtility35.SqlHelper.SQLConnectionString1, CommandType.StoredProcedure,
141
"User_GetValue", sp))
142
{
143
if (rdr.Read())
144
mc = new UserClass(
145
rdr.GetInt32(0),
146
rdr.GetString(1),
147
rdr.GetString(2),
148
rdr.GetString(3)
149
);
150
else
151
mc = new UserClass();
152
}
153
return mc;
154
155
}
156
}
157

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

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

在此类的方法中,调用前面写的存储过程,返回相应的数据。方法写完后,可以使用NUnit或者VS提供的测试类,对方法进行测试。