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





}

