温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:在线考试系统源码及论文
当前文件:
OnlineTestSystem/test.aspx.cs,打开代码结构图
OnlineTestSystem/test.aspx.cs,打开代码结构图1using System; 2
using System.Data; 3
using System.Configuration; 4
using System.Collections; 5
using System.Web; 6
using System.Web.Security; 7
using System.Web.UI; 8
using System.Web.UI.WebControls; 9
using System.Web.UI.WebControls.WebParts; 10
using System.Web.UI.HtmlControls; 11
using System.Data.OleDb; 12
//该源码下载自www.51aspx.com(51aspx.com) 13
14
public partial class test : System.Web.UI.Page 15
{ 16
int numberofti = 11;//生成试题的数量 17
int[] testlist;//存放10个随机数的整型数组 18
DataTable Test = new DataTable();//用于存放随机抽取的试题; 19
OleDbConnection conn;//数据库连接 20
protected string[] userselect = new string[10];//考生选择的答案 21
protected string[] trueanswer = new string[10]; //该题正确的答案 22
protected void Page_Load(object sender, EventArgs e) 23
{ 24
lblname.Text = (string)Session["username"]; 25
lbllogintime.Text = (string)Session["logintime"]; 26
Button1.Visible = false; 27
28
} 29
/// <summary> 30
/// 生成10个随机整数,并存放于数组中 31
/// </summary> 32
/// <param name="sender"></param> 33
/// <param name="e"></param> 34
protected void btnbegin_Click(object sender, EventArgs e) 35
{ 36
testlist = new int[numberofti]; 37
string connecionstring = (string)Application["connectstring"]; 38
conn = new OleDbConnection(connecionstring); 39
string commandtext = "select count(id) from question"; 40
OleDbCommand comm = new OleDbCommand(commandtext); 41
comm.Connection = conn; 42
OleDbDataAdapter da = new OleDbDataAdapter(commandtext, conn); 43
DataTable dt_rowcount = new DataTable("rowcount"); 44
da.Fill(dt_rowcount); 45
int rows = int.Parse(dt_rowcount.Rows[0][0].ToString()); 46
47
for (int i = 0; i < numberofti; i++) 48
{ 49
int temp = GetNumber(rows); 50
while (ArrayHasItem(testlist, temp)) 51
{ 52
temp = GetNumber(rows); 53
54
} 55
testlist[i] = temp; 56
} 57
58
59
60
OleDbConnection newconn = new OleDbConnection((string)Application["connectstring"]); 61
OleDbCommand newcomm = new OleDbCommand(); 62
newcomm.Connection = newconn; 63
newconn.Open(); 64
for (int x = 0; x < numberofti; x++) 65
{ 66
newcomm.CommandText = CreateSQL(testlist[x].ToString()); 67
newcomm.ExecuteNonQuery();//把数组里每一个题的ID所对应的标志位置为1,表示该题已经选上 68
} 69
da = new OleDbDataAdapter("select * from question where hasselected='1'", conn);//选择题目 70
da.Fill(Test);//把选择的题目放入DataTable 71
newcomm.CommandText = "update question set hasselected='0' where hasselected='1'";//把已经选择的题目的标志位置为0,以便下次再选 72
conn.Open(); 73
newcomm.ExecuteNonQuery(); 74
conn.Close(); 75
GridView1.DataSource = Test; 76
GridView1.DataBind(); 77
Button1.Visible = true; 78
int number = Test.Rows.Count; 79
80
} 81
/// <summary> 82
/// 按照随机数组里的ID号,抽取试题 83
/// </summary> 84
/// <param name="temarray"></param> 85
/// <returns></returns> 86
private DataTable GetTestTittle(int[] temarray) 87
{ 88
DataTable testTable = new DataTable();//存放试题的表 89
DataTable temptable = new DataTable(); 90
91
OleDbDataAdapter odap = new OleDbDataAdapter("select top 30 * from question", conn); 92
OleDbCommand cmdselect = new OleDbCommand(); 93
odap.Fill(testTable); 94
return testTable; 95
} 96
/// <summary> 97
/// 根据每一个ID号生成SQL语句 98
/// </summary> 99
/// <param name="id"></param> 100
/// <returns></returns> 101
private string CreateSQL(string id) 102
{ 103
return "update question set hasselected='1' where id=" + id; 104
} 105
/// <summary> 106
/// 得到随机数 107
/// </summary> 108
/// <param name="maxvalue">随机数的最大值</param> 109
/// <returns></returns> 110
private int GetNumber(int maxvalue) 111
{ 112
Random rd = new Random(DateTime.Now.Second); 113
int result = rd.Next(maxvalue + 1); 114
if (result == 0) 115
result = 1; 116
return result; 117
118
} 119
/// <summary> 120
/// 判断一个整数数组里面是否包括一个整数 121
/// </summary> 122
/// <param name="array">数组</param> 123
/// <param name="item">一个整数</param> 124
/// <returns></returns> 125
private bool ArrayHasItem(int[] array, int item) 126
{ 127
for (int i = 0; i < array.Length; i++) 128
{ 129
if (array[i] == item) 130
{ 131
return true; 132
133
} 134
} 135
return false; 136
} 137
/// <summary> 138
/// 退出系统,并转到登陆页面,清除session 139
/// </summary> 140
/// <param name="sender"></param> 141
/// <param name="e"></param> 142
protected void Button2_Click(object sender, EventArgs e) 143
{ 144
Session.Clear(); 145
Response.Redirect("default.aspx"); 146
} 147
/// <summary> 148
/// 交卷 149
/// </summary> 150
/// <param name="sender"></param> 151
/// <param name="e"></param> 152
protected void Button1_Click(object sender, EventArgs e) 153
{ 154
int testnumber = Test.Rows.Count; 155
Response.Write(testnumber.ToString()); 156
157
for (int i = 0; i < testnumber; i++) 158
{ 159
string selvalue = ((RadioButtonList)GridView1.Rows[i].FindControl("RadioButtonList2")).SelectedValue; 160
userselect[i] = selvalue; 161
trueanswer[i] = Test.Rows[i]["answer"].ToString(); 162
} 163
164
} 165
} 166
167





}
}