利用ASP.NET構(gòu)建網(wǎng)上考試系統(tǒng)
2024-07-10 12:55:13
供稿:網(wǎng)友
注冊會員,創(chuàng)建你的web開發(fā)資料庫,隨著計(jì)算機(jī)網(wǎng)絡(luò)的普及,基于數(shù)據(jù)庫的b/s網(wǎng)上考試系統(tǒng)得到廣泛地應(yīng)用,現(xiàn)以asp.net(c#)+sql server(或access)為例說明開發(fā)網(wǎng)上考試系統(tǒng)的實(shí)現(xiàn)方法。
一、數(shù)據(jù)庫的設(shè)計(jì):
建立數(shù)據(jù)庫netexam,在庫中添加考生信息表stuinfo,分別建立以下字段:考號examid(c)(主鍵)、考生姓名name(c)、是否登錄考試logyn(c)、得分score(c)、隨機(jī)生成的試題答案mca(c) (注:此處以多選題為例,單選題、判斷題同理)。添加多選題題庫表mc,建立以下字段:題目question(c)、四個(gè)選choice1(c)、choice2(c)、choice3(c)、choice4(c)、答案answer(c)(注:多選題答案用0表示未選,1表示選擇,如選擇abd就用1101表示)。
二、考生登錄:
在此處將考生信息插入表stuinfo,此處要防止考生重復(fù)登錄!給指定唯一的考號,并將考號字段examid(c)設(shè)為主鍵,當(dāng)重復(fù)登錄時(shí),用catch捕獲錯(cuò)誤,給出相應(yīng)提示,部分代碼如下(login.aspx):
private void butok_click(object sender, system.eventargs e)
{
...
sqlconnection stuconn=new sqlconnection("data source=localhost;integrated security=sspi;
initial catalog=netexam");
sqlcommand logincmd=stuconn.createcommand(); //可根據(jù)不同情況選擇不同的數(shù)據(jù)庫連接
...
logincmd.commandtext="insert into stuinfo(examid,name) values('"+txtid.text.trim()+"','"+txtname.text.trim()+"')";
//將考號、姓名插入相應(yīng)字段,其中txtid,txtname分別是輸入考號和姓名的文本框
try
{
...
session["id"]=txtid.text.trim();
stuconn.open();
logincmd.executereader();
response.redirect("test.aspx");
}
catch(exception) //捕獲相應(yīng)錯(cuò)誤
{
response.write("<script language=/"javascript/">"+"/n");
response.write("alert(/"不能重復(fù)登錄,或考號、姓名、密碼是否有誤!/")"+"/n</script>");
}
...
}
三、試題生成:
1、防止考生刷新頁面:
由于加載試題頁面時(shí)將從數(shù)據(jù)庫中隨機(jī)抽取試題,所以應(yīng)防止考生刷新面頁(刷新頁面會重新生成新的試題)。方法是將表stuinfo中的logyn字段默認(rèn)值設(shè)為0,加載試題后設(shè)為1,交卷評分后設(shè)為2,加載試題頁面時(shí)進(jìn)行相應(yīng)檢查,以防止頁面的刷新,代碼如下(test.aspx):
private void page_load(object sender, system.eventargs e)
{
if(!ispostback)
{
...
questcmd.commandtext="select logyn from stuinfo where examid= '"+ session["id"].tostring()+"'";
questconn.open();
sqldatareader questrd=questcmd.executereader();
questrd.read();
if(questrd["logyn"].tostring().trim().equals("1")||questrd["logyn"].tostring().trim().equals("2"))
//判斷是否已加載試題或是否已評分
{
...
response.write("<script language=/"javascript/">"+"/n");
response.write("alert(/"不能刷新!請與管理員聯(lián)系,重新登錄。/")"+"/n</script>");
...
}
else
{
...
questcmd.commandtext="update stuinfo set logyn='1'"; //已成功加載試題
questcmd.executereader();
...
}
}
...
}
2、隨機(jī)生成試題:
網(wǎng)上考試系統(tǒng)的關(guān)鍵是試題的隨機(jī)生成,即對于不同的計(jì)算機(jī)訪問系統(tǒng)時(shí)將從題庫中隨機(jī)地抽取不同的試題。
在此我們可使用sql語句"select top n * from mc order by newid()"從題庫中隨機(jī)抽取n條記錄,其中newid()生成 uniqueidentifier 值(若是access數(shù)據(jù)庫則用"select top n * from mc order by rnd(id)",其中id為自動(dòng)編號字段)。
在test.aspx頁面上放置一個(gè)panel容器控件,以便動(dòng)態(tài)生成綁定到試題的控件,并將從表mc中隨機(jī)生成的試題答案寫入表stuinfo表中的mca字段,代碼如下(test.aspx):
private void page_load(object sender, system.eventargs e)
{
if(!ispostback)
{
...
questcmd.commandtext="select top 10 * from mc order by newid()";//以隨機(jī)生成10道題為例
questconn.open();
questrd=questcmd.executereader();
while(questrd.read())
{
literal littxt=new literal();
literal litbl=new literal();
checkboxlist chkmc=new checkboxlist();
chkmc.id="chkmc"+i.tostring();
littxt.text=i.tostring()+"、 "+server.htmlencode(questrd["question"].tostring())+"<br><blockquote>";
litbl.text="</blockquote>";
chkmc.font.size=11;
for(int j=1;j<=4;j++)
{
chkmc.items.add(server.htmlencode(questrd["choice"+j.tostring()].tostring()));
chkmc.items[j-1].value=j.tostring();
}
mcstr+=questrd["answer"].tostring().trim(); //mcstr是存儲隨機(jī)生成試題答案的字符串變量
mypanel.controls.add(littxt);
mypanel.controls.add(chkmc);
mypanel.controls.add(litbl);
i++;
}
...
questcmd.commandtext="update stuinfo set mca='"+mcstr+"' where examid= '" +session["id"].tostring()+"'"; //將隨機(jī)生成試題答案寫入數(shù)據(jù)表
questconn.open();
questrd=questcmd.executereader();
...
}
...
}
四、交卷評分:
當(dāng)用戶點(diǎn)擊交卷按鈕后,應(yīng)將用戶的答題結(jié)果與試題答案進(jìn)行比對,并給出相應(yīng)的分值寫入數(shù)據(jù)表,最后將表stuinfo中的logyn字段設(shè)置為2,顯示考試得分,代碼如下(test.aspx):
private void butsend_click(object sender, system.eventargs e)
{
...
for(int i=1;i<=10;i++) //由于隨機(jī)生成了10道題,所以循環(huán)10次
{
for(int j=0;j<4;j++)
if(request.form["chkmc"+i.tostring()+":"+j.tostring()]!=null)
mcs+="1"; //mcs是存儲考生所選答案的字符串變量,已選用"1"表示,未選用"0"表示
else
mcs+="0";
}
...
questcmd.commandtext="select mca from stuinfo where examid= '" +session["id"].tostring()+"'";
questconn.open();
sqldatareader questrd=questcmd.executereader();
questrd.read();
int stuscore=0; //存儲得分的變量
for(i=0;i<10;i+=4)
{
if(questrd["mca"].tostring().substring(i,4).equals(mcs.substring(i,4)))
stuscore+=2; //從字段mca和mcs中每次取四個(gè)字符進(jìn)行比對,如相等則加上2分
}
...
questcmd.commandtext="update stuinfo set score="+stuscore.tostring()+",logyn='2' where examid= '" + session["id"].tostring()+"'and logyn='1'"; //設(shè)置已評分標(biāo)志
questrd=questcmd.executereader();
...
response.redirect("score.aspx"); //顯示考試得分頁面
...
}
限于篇幅,在此只列出了實(shí)現(xiàn)網(wǎng)上考試系統(tǒng)的幾個(gè)要點(diǎn),讀者可根據(jù)自已的需要進(jìn)一步完善相應(yīng)的數(shù)據(jù)驗(yàn)證、后臺管理及界面設(shè)計(jì)。