Asp和Access數(shù)據(jù)庫(kù)實(shí)現(xiàn)的一個(gè)網(wǎng)上考試系統(tǒng)。當(dāng)用戶憑用戶名和口令登錄時(shí),系統(tǒng)首先檢查該用戶是否已參加過考試,若是則進(jìn)行成績(jī)查詢,若否則從題庫(kù)中提取考題供用戶解答。等用戶提交答卷后,系統(tǒng)進(jìn)行評(píng)分并將成績(jī)登記入庫(kù)。 一. 數(shù)據(jù)庫(kù)設(shè)計(jì)
首先建立一數(shù)據(jù)庫(kù)exercise.mdb,其中包括兩個(gè)表:user和test,user表中有包括三個(gè)字段:
字段名稱:user;字段類型:文本;字段大小:20。存儲(chǔ)用戶名
字段名稱:passwd;字段類型:文本;字段大?。?0。存儲(chǔ)用戶密碼
字段名稱:score;字段類型:數(shù)字;字段大小:整型。存儲(chǔ)用戶成績(jī) test表中有五個(gè)字段: 字段名稱:question;字段類型:文本;字段大?。?55。存儲(chǔ)考試題目
字段名稱:a;字段類型:文本;字段大?。?00。存儲(chǔ)選項(xiàng)A的答案
字段名稱:b;字段類型:文本;字段大?。?00。存儲(chǔ)選項(xiàng)B的答案
字段名稱:c;字段類型:文本;字段大?。?00。存儲(chǔ)選項(xiàng)C的答案
字段名稱:d;字段類型:文本;字段大?。?00。存儲(chǔ)選項(xiàng)D的答案
字段名稱:ans;字段類型:文本;字段大小:2。存儲(chǔ)正確答案 二.程序原代碼 下面是該系統(tǒng)中較核心的三段原程序,希望對(duì)開發(fā)類似系統(tǒng)的讀者取到拋磚引玉的作用,讀者也可對(duì)其進(jìn)行改進(jìn)以適應(yīng)自己的系統(tǒng)。
‘Login.asp源程序,檢驗(yàn)用戶是否合法 < %@ Language=VBScript % > < % name=trim(request("name")) passwd=trim(request("passwd")) ‘檢查用戶是否輸入信息 if name< >"" and passwd< >"" then Set conn = Server.CreateObject ("ADODB.Connection") conn.Open "driver={Microsoft Access Driver (*.mdb)};dbq=" & Server.MapPath("exercise.mdb") set rs= server.createobject("adodb.recordset") sql= "select * from user where user='" &name& "' and passwd='" & passwd & "'" ‘檢查用戶的合法性 Set rs= conn.Execute(sql) if not(rs.eof) then 檢查用戶是否已參加過考試, 若是則進(jìn)行成績(jī)查詢 if rs(“score”)< >0 then response.write rs(“user”)& “的考試成績(jī)是”&rs(“score”) else session("pass")=1 session(“user”)=name response.redirect “test.asp” end if else Response.Write "對(duì)不起, 用戶或口令不正確!?。? end if else end if % > < HTML > < HEAD > < META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0" > < TITLE >用戶口令檢查< /TITLE > < /HEAD > < BODY > < FORM action="login.asp" id=FORM1 method=post name=FORM1 > < P title="" > < /P > < P title="" > < /P > < P title="" align=center >用戶: < INPUT id=text1 name=name style="HEIGHT: 22px; WIDTH: 103px" >< /P > < P title="" align=center >口令: < INPUT id=password1 name=passwd style="HEIGHT: 23px; WIDTH: 101px" type=password >< /P > < P title="" align=center > < INPUT id=submit1 name=submit1 type=submit value=" 進(jìn)入 " style="FONT-SIZE: medium; FONT-STYLE: normal; FONT-VARIANT: normal; FONT-WEIGHT: bold" title="" >< /P > < P title="" align=center > < /P > < /FORM > < /BODY > < /HTML >
‘test.asp源程序,從題庫(kù)中提取試題供解答 < %@ Language=VBScript % > < % if session(“pass”)< >1 then response.redirect “login.asp” else end if Set conn = Server.CreateObject("ADODB.Connection") conn.Open "driver={Microsoft Access Driver (*.mdb)};dbq=" & Server.MapPath("exercise.mdb") ‘提取試題 sql="select * from test" Set rs = conn.Execute( sql ) % > ‘計(jì)時(shí)函數(shù) < SCRIPT LANGUAGE="JavaScript" > var isn1=null; var isn2=false; today=new Date();
function stopit(){ if(isn2){ clearTimeout(isn1); } isn2 = false; } function startit(){ stopit(); isnclock(); }
function isnclock(){ var now=new Date(); var hrs=now.getHours(); var min=now.getMinutes(); var sec=now.getSeconds(); document.clckh.disp.value=""+((hrs >12) ? hrs-12 : hrs); document.clckm.disp.value=((min< 10) ? "0" : "")+min; document.clcks.disp.value=((sec< 10) ? "0" : "")+sec; document.clck.disp.value=(hrs >=12) ? "p.m." : "a.m."; isn1=setTimeout("isnclock()",1000); isn2=true; } < /SCRIPT >
< HTML > < HEAD > < META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0" > < /HEAD > < BODY onLoad="startit()" BGCOLOR="FFFFFF" > < center > ‘調(diào)用計(jì)時(shí)函數(shù)顯示時(shí)間 < TABLE BORDER=2 > < TR > < TD >Time< /TD >< TD >Hour< /TD >< TD >Min < /TD >< TD >Sec< /TD >< TD >< /TD > < /TR > < TR > < TD >< /TD > < TD VALIGN=TOP >< FORM NAME="clckh" onSubmit="0" > < INPUT TYPE="text" NAME="disp" SIZE=2 VALUE ="" > < /FORM >< /TD > < TD VALIGN=TOP >< FORM NAME="clckm" onSubmit="0" > < INPUT TYPE="text" NAME="disp" SIZE=2 VALUE ="" > < /FORM >< /TD > < TD VALIGN=TOP >< FORM NAME="clcks" onSubmit="0" > < INPUT TYPE="text" NAME="disp" SIZE=2 VALUE ="" > < /FORM >< /TD > < TD VALIGN=TOP >< FORM NAME="clck" onSubmit="0" > < INPUT TYPE="text" NAME="disp" SIZE=4 VALUE ="" > < /FORM >< /TD >< /TR > < /center > ‘顯示試題供解答 < FORM action="result.asp" id=FORM1 method=post name=FORM1 > < P > < /P > < P > < % i=1 rs.movefirst do while not rs.eof% > < P > < %=rs("question")% >< /P > < TABLE align=center border=1 cellPadding=1 cellSpacing=1 width="80%" > < TR > < TD style="WIDTH: 50%" width="50%" < INPUT name=ans< %=i% > type=radio value="A" > < %=rs("a")% >< /TD > < TD >< INPUT name=ans< %=i% > type=radio value="B" > < %=rs("b")% >< /TD >< /TR > < TR > < TD >< INPUT name=ans< %=i% > type=radio value="C" > < %=rs("c")% >< /TD > < TD >< INPUT name=ans< %=i% > type=radio value="D" > < %=rs("d")% >< /TD > < /TR > < /TABLE > < % i=i+1 rs.movenext loop % > < /P > < P > < /P > < P align=center >< INPUT id=submit1 name=submit1 type=submit value=Submit >< INPUT id=reset1 name=reset1 type=reset value=Reset >< /P >< /FORM > < /BODY > < /HTML >
‘result.asp源程序,對(duì)用戶答卷評(píng)分, 并將分?jǐn)?shù)記錄入庫(kù) < %@ Language=VBScript % > < HTML > < HEAD > < META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0" > < /HEAD > < BODY > < P > < /P > < % name=session(“user”) dim score Set conn = Server.CreateObject("ADODB.Connection") conn.Open "driver= {Microsoft Access Driver (*.mdb)};dbq=" & Server.MapPath("exercise.mdb") sql="select ans from test" Set rs = conn.Execute( sql ) ycorrect=0 rsCount=0 ‘給出正確答案并評(píng)分 Response.Write "正確答案:" Do while not rs.eof Response.Write rs("ans") rsCount=rsCount+1 if Request.Form(rsCount)=rs("ans") then ycorrect=ycorrect+1 end if rs.movenext loop Response.Write "< br >你的答案:" score=int(ycorrect/rscount*100) for i=1 to Request.Form.Count-1 Response.Write Request.Form(i) next Response.Write "< br >" Response.Write "你的成績(jī):"&score if score< 60 then Response.Write " 你不及格!"&"< br >" else if score >=60 Response.Write " 及格"&"< br >" else Response.Write " 優(yōu)秀!" ‘將成績(jī)登記入庫(kù) strSql=”insert into user (result) values (”&score&”) where user=’”&name&”’” conn.execute(strSql) % > < /BODY > < /HTML > |