asp.net网络考试系统分析⑶
<% @ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<Script runat="server">
string Lesson;
string No;
string Name;
OleDbDataReader dr;
string SQL;
OleDbConnection Conn;
OleDbCommand Comm;
void Page_Load(Object Src, EventArgs E) {
Lesson = Request["Lesson"];
No = Request["No"];
Name = Request["Name"];
Conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;"+
"Data Source="+Server.MapPath("test.mdb"));
Conn.Open();
SQL = "Select * from " + Lesson;
//Response.Write(SQL);
Comm = new OleDbCommand(SQL,Conn);
dr = Comm.ExecuteReader();
}
//如果<60分,显示为红色
string TestResult(string inputScore){
int Score = Convert.ToInt32(inputScore);
if (-1 == Score) {
return "你还没有考试";
}
else if(Score < 60) {
return "<FONT Color=Red>" + Score + "</FONT>";
}
else {
return Score.ToString();
}
}
</Script>
<HTML>
<BODY BGCOLOR="BEIGE">
<H2>成绩单<HR></H2>
<%
int Score = 0;
while (dr.Read()) {
if (Request["No" + dr["题号"].ToString()] != null) {
string Sel = Request["No" + dr["题号"].ToString()];
string Ans = dr["解答"].ToString();
//Response.Write(Sel + Ans + "<br>");
if (Ans == Sel)
{
Score = Score + Convert.ToInt32(dr["分数"].ToString());
//Response.Write( "OK" + Score);
}
}
}
//重新建立DataReader对象,需要重新打开Connection对象
Conn.Close();
Conn.Open();
SQL = "Select * From 成绩单 ";
SQL = SQL + "Where 学号=" + "'" + No + "'" + " And 姓名='" + Name + "'";
Comm = new OleDbCommand(SQL,Conn);
OleDbDataReader drScore = Comm.ExecuteReader();
if(drScore.Read()){
if (drScore[Lesson].ToString() == "-1") {
SQL = "update 成绩单 set " + Lesson + " = " + Score.ToString()
+ " where 学号=" + "'" + No + "'"
+ " And 姓名='" + Name + "'";
Conn.Close();
Conn.Open();
Comm = new OleDbCommand(SQL,Conn);
Comm.ExecuteNonQuery();
}
else {
Response.Write ("<script>{alert('你已经考过了')}</script>");
}
}
%>
<%=Name%>同志你好,你的考试成绩统计为:<BR>
<CENTER>
<TABLE BORDER=2 BGCOLOR="#F1F1F1" WIDTH=80%>
<TR BGCOLOR="#DDDDDD"><TD>你参加的科目</TD><TD>你的分数</TD></TR>
<%//重新读取成绩
Conn.Close();
Conn.Open();
SQL = "Select * From 成绩单 ";
SQL = SQL + "Where 学号=" + "'" + No + "'" + " And 姓名='" + Name + "'";
Comm = new OleDbCommand(SQL,Conn);
drScore = Comm.ExecuteReader();
if(drScore.Read()){
%>
<TR><TD>ASP</TD><TD Align=Right><%=TestResult(drScore["ASP"].ToString())%></TD></TR>
<TR><TD>XML</TD><TD Align=Right><%=TestResult(drScore["XML"].ToString())%></TD></TR>
<% } %>
</TABLE></center>
<HR>
<A HREF="login.aspx?txtNo=<%=No%>&txtName=<%=Name%>">参加其他科目考试</A>
</BODY>
</HTML>