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(); //可根据不同情况选择不同的数据库连接 ... LoginCmd.CommandText="Insert into StuInfo(ExamId,Name) values(@#"+TxtId.Text.Trim()+"@#,@#"+TxtName.Text.Trim()+"@#)"; //将考号、姓名插入相应字段,其中TxtId,TxtName分别是输入考号和姓名的文本框 try { ... Session["Id"]=TxtId.Text.Trim(); StuConn.Open(); LoginCmd.ExecuteReader(); Response.Redirect("test.aspx"); } catch(Exception) //捕获相应错误 { Response.Write("<script language=\"javascript\">"+"\n"); Response.Write("alert(\"不能重复登录,或考号、姓名、密码是否有误!\")"+"\n</script>"); } ... } |
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(\"不能刷新!请与管理员联系,重新登录。\")"+"\n</script>"); ... } else { ... QuestCmd.CommandText="update StuInfo set LogYn=@#1@#"; //已成功加载试题 QuestCmd.ExecuteReader(); ... } } ... } |
private void Page_Load(object sender, System.EventArgs e) { if(!IsPostBack) { ... QuestCmd.CommandText="select top 10 * from mc order by newid()";//以随机生成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是存储随机生成试题答案的字符串变量 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()+"@#"; //将随机生成试题答案写入数据表 QuestConn.Open(); QuestRd=QuestCmd.ExecuteReader(); ... } ... } |
private void ButSend_Click(object sender, System.EventArgs e) { ... for(int i=1;i<=10;i++) //由于随机生成了10道题,所以循环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中每次取四个字符进行比对,如相等则加上2分 } ... QuestCmd.CommandText="update StuInfo set score="+StuScore.ToString()+",LogYn=@#2@# where ExamId= @#" + Session["Id"].ToString()+"@#and LogYn=@#1@#"; //设置已评分标志 QuestRd=QuestCmd.ExecuteReader(); ... Response.Redirect("score.aspx"); //显示考试得分页面 ... } |