private void SetPaperContent(List<Int32> list1, List<Int32> list2, List<Int32> list3, List<Int32> list4, List<Int32> list5) { tbSingleDAL singleDAL = new tbSingleDAL(); tbCheckDAL checkDAL = new tbCheckDAL(); tbJudgeDAL judgeDAL = new tbJudgeDAL(); tbBlankDAL blankDAL = new tbBlankDAL(); tbAnswerDAL answerDAl = new tbAnswerDAL(); //单选题 for (int i = 0; i < list1.Count; i++) { tbSingle single = singleDAL.GetModelTran(list1[i], SqlTransaction); //single.selectcount = single.selectcount + 1; //singleDAL.UpdateTran(single, SqlTransaction); paperContent.SRContent.Add(single); } //多选题 for (int i = 0; i < list2.Count; i++) { tbCheck check = checkDAL.GetModelTran(list2[i], SqlTransaction); //check.selectcount = check.selectcount + 1; //checkDAL.UpdateTran(check, SqlTransaction); paperContent.CBContent.Add(check); } //判断题 for (int i = 0; i < list3.Count; i++) { tbJudge judge = judgeDAL.GetModelTran(list3[i], SqlTransaction); //judge.selectcount = judge.selectcount + 1; //judgeDAL.UpdateTran(judge, SqlTransaction); paperContent.JDContent.Add(judge); } //填空题 for (int i = 0; i < list4.Count; i++) { tbBlank blank = blankDAL.GetModelTran(list4[i], SqlTransaction); //blank.selectcount = blank.selectcount + 1; //blankDAL.UpdateTran(blank, SqlTransaction); paperContent.BFContent.Add(blank); } //简答题 for (int i = 0; i < list5.Count; i++) { tbAnswer answer = answerDAl.GetModelTran(list5[i], SqlTransaction); //answer.selectcount = answer.selectcount + 1; //answerDAl.UpdateTran(answer, SqlTransaction); paperContent.SAContent.Add(answer); } }
/// <summary> /// 导入到数据库 /// </summary> /// <param name="dt"></param> /// <returns>返回插入不成功的学号</returns> public static List<String> ExportToDB(DataTable dt) { List<String> list = new List<string>(); tbSingleDAL singleDAL = new tbSingleDAL(); for (int i = 0; i < dt.Rows.Count; i++) { tbSingle single = new tbSingle(); int chapterid = GetChapterID(dt.Rows[i][0].ToString(), dt.Rows[i][1].ToString()); if (chapterid != 0) { single.chapterid = chapterid; } else { list.Add("行" + (i + 2) + "的科目或章节不存在!"); continue; } single.ques = dt.Rows[i][2].ToString().Trim(); single.option_a = dt.Rows[i][3].ToString().Trim(); single.option_b = dt.Rows[i][4].ToString().Trim(); single.option_c = dt.Rows[i][5].ToString().Trim(); single.option_d = dt.Rows[i][6].ToString().Trim(); String diff = dt.Rows[i][7].ToString(); if (diff != "容易" && diff != "较易" && diff != "一般" && diff != "较难" && diff != "困难") { list.Add("行" + (i + 2) + "的难度设置不正确!"); continue; } if (diff == "容易") { single.diff = 1; } if (diff == "较易") { single.diff = 2; } if (diff == "一般") { single.diff = 3; } if (diff == "较难") { single.diff = 4; } if (diff == "困难") { single.diff = 5; } String ans = dt.Rows[i][8].ToString(); if (ans != "A" && ans != "B" && ans != "C" && ans != "D") { list.Add("行" + (i + 2) + "的选项不正确!"); continue; } single.ans = ans; single.questype = 1;//随机类型 //判断题目是否存在 if (DbHelperSQL.Exists("select * from tbSingle where ques=@ques", new SqlParameter("@ques", single.ques))) { list.Add("行" + (i + 2) + "的题目已经存在!"); continue; } singleDAL.Add(single); } return list; }
/// <summary> /// 导入到数据库 /// </summary> /// <param name="dt"></param> /// <returns>返回插入不成功的学号</returns> public static List<String> ExportToDB(DataTable dt) { List<String> list = new List<string>(); tbPaper paper = new tbPaper(); tbSingle single = new tbSingle(); tbCheck check = new tbCheck(); tbJudge judge = new tbJudge(); tbBlank blank = new tbBlank(); tbAnswer answer = new tbAnswer(); tbPaperDAL paperDAL = new tbPaperDAL(); tbSingleDAL singleDAL = new tbSingleDAL(); tbCheckDAL checkDAL = new tbCheckDAL(); tbJudgeDAL judgeDAL = new tbJudgeDAL(); tbBlankDAL blankDAL = new tbBlankDAL(); tbAnswerDAL answerDAL = new tbAnswerDAL(); //取得数据库连接 SqlConnection conn = SQLHelper.GetConnection(); //打开数据库连接 conn.Open(); //创建事务 SqlTransaction SqlTransaction = conn.BeginTransaction(); try { for (int i = 0; i < dt.Rows.Count; i++) { String flag = dt.Rows[i][0].ToString(); String temp = dt.Rows[i][1].ToString(); switch (flag) { case "#科目#": //获取科目id try { int subjectid = (int)DbHelperSQL.GetSingle("select id from tbSubject where subjectname=@subjectname and id in" + MyUtil.GetMySubjectString(), new SqlParameter("@subjectname", dt.Rows[i][1].ToString())); paper.subjectid = subjectid; } catch (Exception) { list.Add("科目不存在或者没有权限!"); continue; } break; case "#标题#": paper.papertitle = dt.Rows[i][1].ToString(); if (String.IsNullOrEmpty(paper.papertitle)) { list.Add("标题设置不正确!"); continue; } break; case "#时间#": try { paper.durationtime = Convert.ToInt32(dt.Rows[i][1].ToString().TrimEnd('分')); } catch (Exception) { list.Add("时间格式不正确!"); continue; } break; case "#单选分值#": try { paper.sr_scoreofeach = Convert.ToInt32(dt.Rows[i][1].ToString().TrimEnd('分')); } catch (Exception) { list.Add("单选分值格式不正确!"); continue; } break; case "#多选分值#": try { paper.cb_scoreofeach = Convert.ToInt32(dt.Rows[i][1].ToString().TrimEnd('分')); } catch (Exception) { list.Add("多选分值格式不正确!"); continue; } break; case "#判断分值#": try { paper.jd_scoreofeach = Convert.ToInt32(dt.Rows[i][1].ToString().TrimEnd('分')); } catch (Exception) { list.Add("判断分值格式不正确!"); continue; } break; case "#填空分值#": try { paper.bf_scoreofeach = Convert.ToInt32(dt.Rows[i][1].ToString().TrimEnd('分')); } catch (Exception) { list.Add("填空分值格式不正确!"); continue; } break; case "#简答分值#": try { paper.sa_scoreofeach = Convert.ToInt32(dt.Rows[i][1].ToString().TrimEnd('分')); } catch (Exception) { list.Add("简答分值格式不正确!"); continue; } break; case "#单选#": single.chapterid = 0;//不属于任何章节 single.diff = 3;//默认难度一般 single.questype = 2;//固定类型 single.ques = dt.Rows[i][1].ToString(); single.option_a = dt.Rows[i][2].ToString(); single.option_b = dt.Rows[i][3].ToString(); single.option_c = dt.Rows[i][4].ToString(); single.option_d = dt.Rows[i][5].ToString(); single.ans = dt.Rows[i][6].ToString(); if (single.ans != "A" && single.ans != "B" && single.ans != "C" && single.ans != "D") { list.Add("行" + (i + 2) + "的单选答案不正确!"); continue; } int singleid = singleDAL.AddTran(single, SqlTransaction); paper.sr_list += singleid + ","; paper.sr_count += 1; break; case "#多选#": check.chapterid = 0;//不属于任何章节 check.diff = 3;//默认难度一般 check.questype = 2;//固定类型 check.ques = dt.Rows[i][1].ToString(); check.option_a = dt.Rows[i][2].ToString(); check.option_b = dt.Rows[i][3].ToString(); check.option_c = dt.Rows[i][4].ToString(); check.option_d = dt.Rows[i][5].ToString(); check.option_e = dt.Rows[i][6].ToString(); check.option_f = dt.Rows[i][7].ToString(); check.option_g = dt.Rows[i][8].ToString(); check.ans = dt.Rows[i][9].ToString(); if (check.ans == "" || check.ans == null) { list.Add("行" + (i + 2) + "的多选答案不正确!"); continue; } int checkid = checkDAL.AddTran(check, SqlTransaction); paper.cb_list += checkid + ","; paper.cb_count += 1; break; case "#判断#": judge.chapterid = 0;//不属于任何章节 judge.diff = 3;//默认难度一般 judge.questype = 2;//固定类型 judge.ques = dt.Rows[i][1].ToString(); judge.ans = dt.Rows[i][2].ToString(); if (judge.ans != "对" && judge.ans != "错") { list.Add("行" + (i + 2) + "的判断答案不正确!"); continue; } int judgeid = judgeDAL.AddTran(judge, SqlTransaction); paper.jd_list += judgeid + ","; paper.jd_count += 1; break; case "#填空#": blank.chapterid = 0;//不属于任何章节 blank.diff = 3;//默认难度一般 blank.questype = 2;//固定类型 blank.ques = dt.Rows[i][1].ToString(); blank.ans = dt.Rows[i][2].ToString(); blank.blanklength = blank.ans.Length; int blankid = blankDAL.AddTran(blank, SqlTransaction); paper.bf_list += blankid + ","; paper.bf_count += 1; break; case "#简答#": answer.chapterid = 0;//不属于任何章节 answer.diff = 3;//默认难度一般 answer.questype = 2;//固定类型 answer.ques = dt.Rows[i][1].ToString(); answer.ans = dt.Rows[i][2].ToString(); int answerid = answerDAL.AddTran(answer, SqlTransaction); paper.sa_list += answerid + ","; paper.sa_count += 1; break; } } if (list.Count == 0) { if (paper.sr_list.EndsWith(",")) { paper.sr_list = paper.sr_list.TrimEnd(','); } if (paper.cb_list.EndsWith(",")) { paper.cb_list = paper.cb_list.TrimEnd(','); } if (paper.jd_list.EndsWith(",")) { paper.jd_list = paper.jd_list.TrimEnd(','); } if (paper.bf_list.EndsWith(",")) { paper.bf_list = paper.bf_list.TrimEnd(','); } if (paper.sa_list.EndsWith(",")) { paper.sa_list = paper.sa_list.TrimEnd(','); } paper.allscore = paper.sr_count * paper.sr_scoreofeach + paper.cb_count * paper.cb_scoreofeach + paper.jd_count * paper.jd_scoreofeach + paper.bf_count * paper.bf_scoreofeach + paper.sa_count * paper.sa_scoreofeach; paper.papertype = 2;//固定试卷 paper.createtime = DateTime.Now; paper.userid = ((tbUser)HttpContext.Current.Session[Constant.User]).id; paperDAL.AddTran(paper, SqlTransaction); SqlTransaction.Commit(); } } catch (Exception) { try { SqlTransaction.Rollback(); } catch (Exception) { //事务回滚出错 } } finally { //关闭各种资源 SqlTransaction.Dispose(); conn.Close(); } return list; }
/// <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; }