温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:Asp.net在线投票系统(51aspx版)源码
当前文件:
OnlineVote/App_Code/User.cs[8K,2009-6-12 11:51:27],打开代码结构图
OnlineVote/App_Code/User.cs[8K,2009-6-12 11:51:27],打开代码结构图1using System; 2
using System.Data; 3
using System.Configuration; 4
using System.Web; 5
using System.Web.Security; 6
using System.Web.UI; 7
using System.Web.UI.WebControls; 8
using System.Web.UI.WebControls.WebParts; 9
using System.Web.UI.HtmlControls; 10
using System.Data.SqlClient; 11
12
public interface IUser 13
{ 14
/// <summary> 15
/// 使用存储过程实现用户登录 16
/// </summary> 17
/// <param name="sUserName"></param> 18
/// <param name="sPassword"></param> 19
/// <returns></returns> 20
SqlDataReader GetUserLogin(string sUserName,string sPassword); 21
22
/// <summary> 23
/// 获取所有用户信息 24
/// </summary> 25
/// <returns></returns> 26
SqlDataReader GetUsers(); 27
28
/// <summary> 29
/// 获取单个用户信息 30
/// </summary> 31
/// <param name="nUserID"></param> 32
/// <returns></returns> 33
SqlDataReader GetSingleUser(int nUserID); 34
35
/// <summary> 36
/// 注册一个新用户 37
/// </summary> 38
/// <param name="sUserName"></param> 39
/// <param name="sPassword"></param> 40
/// <param name="sEmail"></param> 41
/// <returns></returns> 42
int AddUser(string sUserName,string sPassword,string sEmail); 43
44
/// <summary> 45
/// 修改用户的信息 46
/// </summary> 47
/// <param name="nUserID"></param> 48
/// <param name="sEmail"></param> 49
/// <returns></returns> 50
int UpdateUser(int nUserID,string sEmail); 51
52
/// <summary> 53
/// 修改用户密码 54
/// </summary> 55
/// <param name="nUserID"></param> 56
/// <param name="sPassword"></param> 57
/// <returns></returns> 58
int UpdateUserPwd(int nUserID,string sPassword); 59
60
/// <summary> 61
/// 删除用户 62
/// </summary> 63
/// <param name="nUserID"></param> 64
/// <returns></returns> 65
int DeleteUser(int nUserID); 66
} 67
68
/// <summary> 69
/// User 的摘要说明 70
/// </summary> 71
public class User : IUser 72
{ 73
private static readonly string GETUSERS = "SELECT * FROM Users"; 74
private static readonly string GETSINGLEUSER = "SELECT * FROM Users WHERE UserID="; 75
private static readonly string ADDUSER = "INSERT INTO Users(UserName,Password,Email,IsAdmin)VALUES"; 76
private static readonly string UPDATEUSER = "UPDATE Users SET Email="; 77
private static readonly string UPDATEUSERADMIN = "UPDATE Users SET IsAdmin="; 78
private static readonly string UPDATEUSERPASSWORD = "UPDATE Users SET Password="; 79
private static readonly string DELETEUSER = "DELETE Users WHERE UserID="; 80
private static readonly string GETUSERLOGINBYSQL = "SELECT UserID FROM Users WHERE UserName ="; 81
82
public User() 83
{ 84
/// 85
} 86
87
public SqlDataReader GetUserLogin(string sUserName,string sPassword) 88
{ 89
///创建链接 90
SqlConnection myConnection = new SqlConnection( 91
ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString); 92
93
///创建Command 94
SqlCommand myCommand = new SqlCommand("Pr_GetUserLogin",myConnection); 95
///设置为执行存储过程 96
myCommand.CommandType = CommandType.StoredProcedure; 97
98
///添加存储过程的参数 99
SqlParameter pUserName = new SqlParameter("@UserName",SqlDbType.VarChar,32); 100
pUserName.Value = sUserName; 101
myCommand.Parameters.Add(pUserName); 102
103
SqlParameter pPassword = new SqlParameter("@Password",SqlDbType.VarChar,255); 104
pPassword.Value = sPassword; 105
myCommand.Parameters.Add(pPassword); 106
107
///定义DataReader 108
SqlDataReader dr = null; 109
try 110
{ 111
///打开链接 112
myConnection.Open(); 113
///读取数据 114
dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection); 115
} 116
catch(SqlException ex) 117
{ 118
///抛出异常 119
throw new Exception(ex.Message,ex); 120
} 121
///返回DataReader 122
return dr; 123
} 124
125
public SqlDataReader GetUsers() 126
{ 127
///创建链接 128
SqlConnection myConnection = new SqlConnection( 129
ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString); 130
///创建Command 131
SqlCommand myCommand = new SqlCommand(GETUSERS,myConnection); 132
133
///定义DataReader 134
SqlDataReader dr = null; 135
try 136
{ 137
///打开链接 138
myConnection.Open(); 139
///读取数据 140
dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection); 141
} 142
catch(SqlException ex) 143
{ 144
///抛出异常 145
throw new Exception(ex.Message,ex); 146
} 147
///返回DataReader 148
return dr; 149
} 150
151
public SqlDataReader GetSingleUser(int nUserID) 152
{ 153
///创建链接 154
SqlConnection myConnection = new SqlConnection( 155
ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString); 156
157
///定义SQL语句 158
string cmdText = GETSINGLEUSER + "'" + nUserID.ToString() + "'"; 159
///创建Command 160
SqlCommand myCommand = new SqlCommand(cmdText,myConnection); 161
162
///定义DataReader 163
SqlDataReader dr = null; 164
try 165
{ 166
///打开链接 167
myConnection.Open(); 168
///读取数据 169
dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection); 170
} 171
catch(SqlException ex) 172
{ 173
///抛出异常 174
throw new Exception(ex.Message,ex); 175
} 176
///返回DataReader 177
return dr; 178
} 179
180
public int AddUser(string sUserName,string sPassword,string sEmail) 181
{ 182
///创建链接 183
SqlConnection myConnection = new SqlConnection( 184
ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString); 185
186
///定义SQL语句 187
string cmdText = ADDUSER + "(" 188
+ "'" + sUserName + "'," 189
+ "'" + sPassword + "'," 190
+ "'" + sEmail + "'," 191
+ "'0'" 192
+ ")"; 193
///创建Command 194
SqlCommand myCommand = new SqlCommand(cmdText,myConnection); 195
196
///定义返回值 197
int nResult = -1; 198
199
try 200
{ 201
///打开链接 202
myConnection.Open(); 203
///执行SQL语句 204
nResult = myCommand.ExecuteNonQuery(); 205
} 206
catch(SqlException ex) 207
{ 208
///抛出异常 209
throw new Exception(ex.Message,ex); 210
} 211
finally 212
{ ///关闭链接 213
myConnection.Close(); 214
} 215
///返回nResult 216
return nResult; 217
} 218
219
public int UpdateUser(int nUserID,string sEmail) 220
{ 221
///创建链接 222
SqlConnection myConnection = new SqlConnection( 223
ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString); 224
225
///定义SQL语句 226
string cmdText = UPDATEUSER 227
+ "'" + sEmail + "'" 228
+ " WHERE UserID=" + "'" 229
+ nUserID.ToString() + "'"; 230
///创建Command 231
SqlCommand myCommand = new SqlCommand(cmdText,myConnection); 232
233
///定义返回值 234
int nResult = -1; 235
236
try 237
{ 238
///打开链接 239
myConnection.Open(); 240
///执行SQL语句 241
nResult = myCommand.ExecuteNonQuery(); 242
} 243
catch(SqlException ex) 244
{ 245
///抛出异常 246
throw new Exception(ex.Message,ex); 247
} 248
finally 249
{ ///关闭链接 250
myConnection.Close(); 251
} 252
///返回nResult 253
return nResult; 254
} 255
256
public int UpdateUserPwd(int nUserID,string sPassword) 257
{ 258
///创建链接 259
SqlConnection myConnection = new SqlConnection( 260
ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString); 261
262
///定义SQL语句 263
string cmdText = UPDATEUSERPASSWORD 264
+ "'" + sPassword + "'" 265
+ " WHERE UserID=" + "'" 266
+ nUserID.ToString() + "'"; 267
///创建Command 268
SqlCommand myCommand = new SqlCommand(cmdText,myConnection); 269
270
///定义返回值 271
int nResult = -1; 272
273
try 274
{ 275
///打开链接 276
myConnection.Open(); 277
///执行SQL语句 278
nResult = myCommand.ExecuteNonQuery(); 279
} 280
catch(SqlException ex) 281
{ 282
///抛出异常 283
throw new Exception(ex.Message,ex); 284
} 285
finally 286
{ ///关闭链接 287
myConnection.Close(); 288
} 289
///返回nResult 290
return nResult; 291
} 292
293
public int UpdateUserAdmin(int nUserID,bool bIsAdmin) 294
{ 295
///创建链接 296
SqlConnection myConnection = new SqlConnection( 297
ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString); 298
299
///定义SQL语句 300
string cmdText = UPDATEUSERADMIN 301
+ "'" + (bIsAdmin ? 1 : 0).ToString() + "'" 302
+ " WHERE UserID=" + "'" 303
+ nUserID.ToString() + "'"; 304
///创建Command 305
SqlCommand myCommand = new SqlCommand(cmdText,myConnection); 306
307
///定义返回值 308
int nResult = -1; 309
310
try 311
{ 312
///打开链接 313
myConnection.Open(); 314
///执行SQL语句 315
nResult = myCommand.ExecuteNonQuery(); 316
} 317
catch(SqlException ex) 318
{ 319
///抛出异常 320
throw new Exception(ex.Message,ex); 321
} 322
finally 323
{ ///关闭链接 324
myConnection.Close(); 325
} 326
///返回nResult 327
return nResult; 328
} 329
330
public int DeleteUser(int nUserID) 331
{ 332
///创建链接 333
SqlConnection myConnection = new SqlConnection( 334
ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString); 335
336
///定义SQL语句 337
string cmdText = DELETEUSER 338
+ "'" + nUserID.ToString() + "'"; 339
///创建Command 340
SqlCommand myCommand = new SqlCommand(cmdText,myConnection); 341
342
///定义返回值 343
int nResult = -1; 344
345
try 346
{ 347
///打开链接 348
myConnection.Open(); 349
///执行SQL语句 350
nResult = myCommand.ExecuteNonQuery(); 351
} 352
catch(SqlException ex) 353
{ 354
///抛出异常 355
throw new Exception(ex.Message,ex); 356
} 357
finally 358
{ ///关闭链接 359
myConnection.Close(); 360
} 361
///返回nResult 362
return nResult; 363
} 364
} 365






}