protected void lbtnAdd_Click(object sender, EventArgs e) { int answerofpaperid = Convert.ToInt32(ViewState["answerofpaperid"].ToString()); int userid = Convert.ToInt32(ViewState["userid"].ToString()); tbAnswerOfPaper model = answerOfPaperDAL.GetModel(answerofpaperid); model.getscore = Convert.ToDecimal(ddlGetScore.SelectedValue); answerOfPaperDAL.Update(model); //判断这个学生的简答题是否全部改完 if (!DbHelperSQL.Exists("select * from tbAnswerOfPaper where arrangeid=" + arrangeid + " and userid=" + userid + " and getscore=-1")) { //批改完毕 double sum = (double)DbHelperSQL.GetSingle("select sum(getscore) from tbAnswerOfPaper where arrangeid=" + arrangeid + " and userid=" + userid); int scoreid = (int)DbHelperSQL.GetSingle("select id from tbScore where arrangeid=" + arrangeid + " and userid=" + userid); tbScore score = scoreDAL.GetModel(scoreid); score.hascorrect = 1; score.scorestatus = 3; score.score += Convert.ToDecimal(sum); scoreDAL.Update(score); } BindData(); }
/// <summary> /// 获取正确答案,计算分数,更新题库selectcount和rightcount /// </summary> /// <param name="studentAnswer"></param> /// <returns></returns> private PaperContent GetStandardAnswer(PaperContent studentAnswer) { PaperContent standardAnswer = new PaperContent(); tbSingleDAL singleDAL = new tbSingleDAL(); tbCheckDAL checkDAL = new tbCheckDAL(); tbJudgeDAL judgeDAL = new tbJudgeDAL(); tbBlankDAL blankDAL = new tbBlankDAL(); tbAnswerDAL answerDAl = new tbAnswerDAL(); tbAnswerOfPaperDAL answerOfPaperDAL = new tbAnswerOfPaperDAL(); //取得数据库连接 SqlConnection conn = SQLHelper.GetConnection(); //打开数据库连接 conn.Open(); //创建事务 SqlTransaction SqlTransaction = conn.BeginTransaction(); try { //单选 foreach (tbSingle item in studentAnswer.SRContent) { tbSingle single = singleDAL.GetModelTran(item.id, SqlTransaction); single.selectcount = single.selectcount + 1; if (single.ans == item.ans) { //正确 sum += paper.sa_scoreofeach; single.rightcount = single.rightcount + 1; } standardAnswer.SRContent.Add(single); singleDAL.UpdateTran(single, SqlTransaction); } //多选 foreach (tbCheck item in studentAnswer.CBContent) { tbCheck check = new tbCheckDAL().GetModelTran(item.id, SqlTransaction); check.selectcount = check.selectcount + 1; if (check.ans == item.ans) { //正确 sum += paper.cb_scoreofeach; check.rightcount = check.rightcount + 1; } standardAnswer.CBContent.Add(check); checkDAL.UpdateTran(check, SqlTransaction); } //判断 foreach (tbJudge item in studentAnswer.JDContent) { tbJudge judge = new tbJudgeDAL().GetModelTran(item.id, SqlTransaction); judge.selectcount = judge.selectcount + 1; if (judge.ans == item.ans) { //正确 sum += paper.jd_scoreofeach; judge.rightcount = judge.rightcount + 1; } standardAnswer.JDContent.Add(judge); judgeDAL.UpdateTran(judge, SqlTransaction); } //填空 foreach (tbBlank item in studentAnswer.BFContent) { tbBlank blank = new tbBlankDAL().GetModelTran(item.id, SqlTransaction); blank.selectcount = blank.selectcount + 1; if (blank.ans == item.ans) { //正确 sum += paper.bf_scoreofeach; blank.rightcount = blank.rightcount + 1; } standardAnswer.BFContent.Add(blank); blankDAL.UpdateTran(blank, SqlTransaction); } //简答题 不比较答案,但返回正确答案 并记录学生答案 foreach (tbAnswer item in studentAnswer.SAContent) { tbAnswer answer = new tbAnswerDAL().GetModelTran(item.id, SqlTransaction); standardAnswer.SAContent.Add(answer); if (arrange.arrangetype == 2) { //插入学生答案 tbAnswerOfPaper answerOfPaper = new tbAnswerOfPaper(); answerOfPaper.answerid = answer.id; answerOfPaper.arrangeid = arrange.id; answerOfPaper.useranswer = item.ans; answerOfPaper.userid = score.userid; answerOfPaper.allscore = paper.sa_scoreofeach; answerOfPaper.getscore = -1;//还没批改 //删除已存在的 String deleteSql = "delete from tbAnswerOfPaper where answerid=" + answerOfPaper.answerid + " and arrangeid=" + answerOfPaper.arrangeid + " and userid=" + answerOfPaper.userid; SqlHelper.ExecuteNonQuery(SqlTransaction, CommandType.Text, deleteSql); answerOfPaperDAL.AddTran(answerOfPaper, SqlTransaction); } } SqlTransaction.Commit(); } catch (Exception) { try { SqlTransaction.Rollback(); } catch (Exception) { //事务回滚出错 } } finally { //关闭各种资源 SqlTransaction.Dispose(); conn.Close(); } return(standardAnswer); }