users实体类包括调用DBHelper中的各种方法,但是没有返回DataTable的方法
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Data.SqlClient;
6 using System.Data;
7
8 /// <summary>
9 ///Users 的摘要说明
10 /// </summary>
11 public class Users
12 {
13 //声明7个私有字段
14 private int userID;
15 private string userName;
16 private string password;
17 private string phone;
18 private string email;
19 private string address;
20 private string province;
21 //声明7个公共属性
22 public int UserID
23 {
24 get
25 {
26 return this.userID;
27 }
28 set { this.userID = value; }
29
30 }
31 public string UserName
32 {
33 get { return this.userName; }
34 set { this.userName = value; }
35 }
36 public string Password
37 {
38 get { return this.password; }
39 set { this.password = value; }
40 }
41 public string Phone
42 {
43 get { return this.phone; }
44 set { this.phone = value; }
45 }
46 public string Email
47 {
48 get { return this.email; }
49 set { this.email = value; }
50 }
51 public string Address
52 {
53 get { return this.address; }
54 set { this.address = value; }
55 }
56 public string Province
57 {
58 get { return this.province; }
59 set { this.province = value; }
60 }
61 public Users()
62 {
63 //
64 //TODO: 在此处添加构造函数逻辑
65 //
66 }
67 //重载构造函数
68 public Users(int UserId, string UserName, string UserPwd, string Phone, string Email, string Address, string Province)
69 {
70 this.UserID = UserId;
71 this.UserName = UserName;
72 this.Password = UserPwd;
73 this.Phone = Phone;
74 this.Email = Email;
75 this.Address = Address;
76 this.Province = Province;
77 }
78 public Users(SqlDataReader Dr)
79 {
80 this.UserID = Convert.ToInt32(Dr["UserID"]);
81 this.UserName = Dr["UserName"].ToString();
82 this.Password = Dr["Password"].ToString();
83 this.Phone = Dr["Phone"].ToString();
84 this.Email = Dr["Email"].ToString();
85 this.Address = Dr["Address"].ToString();
86 this.Province = Dr["Province"].ToString();
87 }
88
89 //添加公共静态方法
90
91 //添加用户
92 public static bool AddUser(Users singleUser)
93 {
94 bool Flage = false;
95 string procname = "dbo.AddUser";
96
97 //参数定义及赋值
98 SqlParameter pramUserName = new SqlParameter("@userName", SqlDbType.NVarChar, 50);
99 pramUserName.Value = singleUser.UserName;
100 SqlParameter pramPassword = new SqlParameter("@password", SqlDbType.NVarChar, 50);
101 pramPassword.Value = singleUser.Password;
102 SqlParameter pramPhone = new SqlParameter("@phone", SqlDbType.NVarChar, 50);
103 pramPhone.Value = singleUser.Phone;
104 SqlParameter pramAddress = new SqlParameter("@address", SqlDbType.NVarChar, 100);
105 pramAddress.Value = singleUser.Address;
106 SqlParameter pramEmail = new SqlParameter("@email", SqlDbType.NVarChar, 100);
107 pramEmail.Value = singleUser.Email;
108 SqlParameter pramProvince = new SqlParameter("@province", SqlDbType.NVarChar, 50);
109 pramProvince.Value = singleUser.Province;
110 //保存参数进数组
111 SqlParameter[] prams = {pramUserName,pramPassword,pramPhone,pramAddress,pramEmail,pramProvince};
112 //执行
113 int intResult = DBHelper.RunExecute(procname, prams);
114 if (intResult > 0)
115 {
116 Flage = true;
117 }
118 return Flage;
119 }
120 //删除用户
121 public static bool DeleteUser(Users singleUser)
122 {
123 bool Flage = false;
124 string procname = "dbo.DeleteUser";
125
126 //参数定义及赋值
127 SqlParameter pramUserID = new SqlParameter("@userID", SqlDbType.Int);
128 pramUserID.Value = singleUser.UserID;
129 //保存参数进数组
130 SqlParameter[] prams = { pramUserID };
131 //执行
132 int intResult = DBHelper.RunExecute(procname, prams);
133 if (intResult > 0)
134 {
135 Flage = true;
136 }
137 return Flage;
138 }
139 //更新用户信息
140 public static bool UpdateUser(Users singleUser)
141 {
142 bool Flage = false;
143 string procname = "dbo.UpdateUser";
144
145 //参数定义及赋值
146 SqlParameter pramUserName = new SqlParameter("@userName", SqlDbType.NVarChar, 50);
147 pramUserName.Value = singleUser.UserName;
148 SqlParameter pramPassword = new SqlParameter("@password", SqlDbType.NVarChar, 50);
149 pramPassword.Value = singleUser.Password;
150 SqlParameter pramPhone = new SqlParameter("@phone", SqlDbType.NVarChar, 50);
151 pramPhone.Value = singleUser.Phone;
152 SqlParameter pramAddress = new SqlParameter("@address", SqlDbType.NVarChar, 100);
153 pramAddress.Value = singleUser.Address;
154 SqlParameter pramEmail = new SqlParameter("@email", SqlDbType.NVarChar, 100);
155 pramEmail.Value = singleUser.Email;
156 SqlParameter pramProvince = new SqlParameter("@province", SqlDbType.NVarChar, 50);
157 pramProvince.Value = singleUser.Province;
158 SqlParameter pramUserID = new SqlParameter("@userID", SqlDbType.Int);
159 pramUserID.Value = singleUser.UserID;
160 //保存参数进数组
161 SqlParameter[] prams = { pramUserName, pramPassword, pramPhone, pramAddress, pramEmail, pramProvince,pramUserID };
162 //执行
163 int intResult = DBHelper.RunExecute(procname, prams);
164 if (intResult > 0)
165 {
166 Flage = true;
167 }
168 return Flage;
169 }
170 //修改密码
171 public static bool ChangePwd(int UserId, string UserPwd)
172 {
173 bool Flage = false;
174 string procname = "dbo.ChangePwd";
175
176 //参数定义及赋值
177 SqlParameter pramUserID = new SqlParameter("@userID", SqlDbType.Int);
178 pramUserID.Value = UserId;
179 SqlParameter pramPassword = new SqlParameter("@password", SqlDbType.NVarChar, 50);
180 pramPassword.Value = UserPwd;
181 //保存参数进数组
182 SqlParameter[] prams = {pramPassword, pramUserID };
183 //执行
184 int intResult = DBHelper.RunExecute(procname, prams);
185 if (intResult > 0)
186 {
187 Flage = true;
188 }
189 return Flage;
190 }
191 //验证用户
192 public static bool ValidateUser(string UserName, string UserPwd)
193 {
194 bool Flage = false;
195 string procname = "dbo.ValidateUser";
196 //参数定义及赋值
197 SqlParameter pramUserName = new SqlParameter("@userName", SqlDbType.NVarChar, 50);
198 pramUserName.Value = UserName;
199 SqlParameter pramPassword = new SqlParameter("@password", SqlDbType.NVarChar, 50);
200 pramPassword.Value = UserPwd;
201 //保存参数进数组
202 SqlParameter[] prams = { pramPassword, pramUserName };
203 //执行
204 int intResult = DBHelper.RunExecuteScalar(procname, prams);
205 if (intResult > 0)
206 {
207 Flage = true;
208 }
209 return Flage;
210 }
211 //验证用户名是否存在
212 public static bool IsValidateUserName(string UserName)
213 {
214 bool Flage = false;
215 string procname = "dbo.IsValidateUserName";
216 //参数定义及赋值
217 SqlParameter pramUserName = new SqlParameter("@userName", SqlDbType.NVarChar, 50);
218 pramUserName.Value = UserName;
219 //保存参数进数组
220 SqlParameter[] prams = { pramUserName };
221 //执行
222 int intResult = DBHelper.RunExecuteScalar(procname, prams);
223 if (intResult > 0)
224 {
225 Flage = true;
226 }
227 return Flage;
228 }
229 //返回所有用户,用泛型List<Users>
230 public static List<Users> GetAllUsers()
231 {
232 List<Users> allUsers = new List<Users>();
233 string procname = "dbo.GetAllUsers";
234 SqlDataReader Dr = DBHelper.RunProcGetReader(procname);
235 while (Dr.Read())
236 {
237 allUsers.Add(new Users(Dr));
238 }
239 Dr.Close();
240 return allUsers;
241 }
242 public static string GetUserNameByUserId(int UserId)
243 {
244 string strUserName = string.Empty;
245 string procname = "dbo.GetUserNameByUserId";
246 //参数定义及赋值
247 SqlParameter pramUserID = new SqlParameter("@userID", SqlDbType.Int);
248 pramUserID.Value = UserId;
249 //保存参数进数组
250 SqlParameter[] prams = { pramUserID };
251 //执行
252 SqlDataReader Dr = DBHelper.RunProcGetReader(procname, prams);
253 while (Dr.Read())
254 {
255 strUserName = Dr["UserName"].ToString();
256 }
257 Dr.Close();
258 return strUserName;
259 }
260 public static Users GetUserByUserId(int UserId)
261 {
262 Users singleUser = null;
263 string procname = "dbo.GetUserByUserId";
264 //参数定义及赋值
265 SqlParameter pramUserID = new SqlParameter("@userID", SqlDbType.Int);
266 pramUserID.Value = UserId;
267 //保存参数进数组
268 SqlParameter[] prams = { pramUserID };
269 //执行
270 SqlDataReader Dr = DBHelper.RunProcGetReader(procname, prams);
271 while (Dr.Read())
272 {
273 singleUser = new Users(Dr);
274 }
275 Dr.Close();
276 return singleUser;
277 }
278 public static int GetUserIdByUserName(string UserName)
279 {
280 int intUserId = 0;
281 string procname = "dbo.GetUserIdByUserName";
282 //参数定义及赋值
283 SqlParameter pramUserName = new SqlParameter("@userName", SqlDbType.NVarChar, 50);
284 pramUserName.Value = UserName;
285 //保存参数进数组
286 SqlParameter[] prams = { pramUserName };
287 //执行
288 SqlDataReader Dr = DBHelper.RunProcGetReader(procname, prams);
289 while (Dr.Read())
290 {
291 intUserId = Convert.ToInt32(Dr["UserID"]);
292 }
293 Dr.Close();
294 return intUserId;
295 }
296 public static string GetProvinceByUserID(int userID)
297 {
298 string useProvince = "";
299 string procname = "dbo.GetProvinceByUserID";
300
301 SqlParameter pramUserID = new SqlParameter("@userID", SqlDbType.Int);
302 pramUserID.Value = userID;
303 //保存参数进数组
304 SqlParameter[] prams = { pramUserID };
305 //执行
306 SqlDataReader Dr = DBHelper.RunProcGetReader(procname, prams);
307 while (Dr.Read())
308 {
309 useProvince = Dr["Province"].ToString().Trim();
310 }
311 Dr .Close();
312 return useProvince;
313 }
314 }