/// <summary> /// 保存 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnEnter_Click(object sender, EventArgs e) { Song.Entities.Questions mm = id <= 0 ? new Song.Entities.Questions() : Business.Do <IQuestions>().QuesSingle(id); //题型、学科、题干 mm.Qus_Type = this.type == 0 ? ddlType.SelectedIndex + 1 : this.type; mm.Qus_IsUse = cbIsUse.Checked; mm.Sbj_ID = SortSelect1.SbjID; mm.Sbj_Name = SortSelect1.SbjName; mm.Cou_ID = SortSelect1.CouID; mm.Ol_ID = SortSelect1.OlID; mm.Qus_Title = tranTxt(tbTitle.Text); //排序号 int tax = 0; int.TryParse(tbTax.Text, out tax); mm.Qus_Tax = tax; //难度 mm.Qus_Diff = Convert.ToInt32(ddlDiff.SelectedItem.Value); //资料、讲解 if (tbKnID.Text != string.Empty) { mm.Kn_ID = Convert.ToInt32(tbKnID.Text); } mm.Qus_Explain = tbExplan.Text.Trim(); //UID mm.Qus_UID = getUID(); //是否处理报错信息 if (cbWrong.Checked) { mm.Qus_IsWrong = false; } //选择项 List <Song.Entities.QuesAnswer> qans = new List <QuesAnswer>(); for (int i = 0; i < gvAnswer.Rows.Count; i++) { //单选钮 RadioButton rb = (RadioButton)gvAnswer.Rows[i].FindControl("rbAns"); //选项文本框 TextBox tb = (TextBox)gvAnswer.Rows[i].FindControl("itemTxt"); //主键 int ansid = 0; string keyId = gvAnswer.DataKeys[gvAnswer.Rows[i].RowIndex].Value.ToString(); int.TryParse(keyId, out ansid); //如果文本框为空,则跳过 if (tb.Text.Trim() == "") { continue; } //创建选项的对象 Song.Entities.QuesAnswer ans = new Song.Entities.QuesAnswer(); ans.Ans_Context = tb.Text.Trim(); ans.Ans_IsCorrect = rb.Checked; ans.Qus_ID = id; ans.Qus_UID = getUID(); ans.Ans_ID = ansid; if (ans.Ans_ID <= 0) { ans.Ans_ID = new Random((i + 1) * DateTime.Now.Millisecond).Next(1, 1000); } qans.Add(ans); } mm.Qus_Items = Business.Do <IQuestions>().AnswerToItems(qans.ToArray()); //确定操作 try { if (id == 0) { id = Business.Do <IQuestions>().QuesAdd(mm); } else { Business.Do <IQuestions>().QuesSave(mm); } Master.AlertCloseAndRefresh("操作成功!"); } catch (Exception ex) { Master.Alert(ex.Message); } }
/// <summary> /// 将某一行数据加入到数据库 /// </summary> /// <param name="dr"></param> /// <param name="dl"></param> private void _inputData(DataRow dr) { Song.Entities.Questions obj = new Song.Entities.Questions(); obj.Qus_IsUse = true; obj.Qus_Type = this.type; foreach (KeyValuePair <String, String> rel in ExcelInput1.DataRelation) { //Excel的列的值 string column = dr[rel.Key].ToString(); //数据库字段的名称 string field = rel.Value; if (field == "Qus_ID") { if (column == string.Empty || column.Trim() == "") { continue; } int ques = Convert.ToInt32(column); Song.Entities.Questions isHavObj = Business.Do <IQuestions>().QuesSingle(ques); if (isHavObj != null) { obj = isHavObj; } } //题干难度、专业、试题讲解 if (field == "Qus_Title") { if (string.IsNullOrEmpty(column) || column.Trim() == "") { return; } obj.Qus_Title = column; } if (field == "Qus_Diff") { obj.Qus_Diff = Convert.ToInt16(column); } if (field == "Sbj_Name") { Song.Entities.Subject subject = Business.Do <ISubject>().SubjectBatchAdd(org.Org_ID, column); if (subject != null) { obj.Sbj_Name = subject.Sbj_Name; obj.Sbj_ID = subject.Sbj_ID; } } if (field == "Cou_Name") { Song.Entities.Course course = Business.Do <ICourse>().CourseBatchAdd(org.Org_ID, obj.Sbj_ID, column); if (course != null) { obj.Cou_ID = course.Cou_ID; } } if (field == "Ol_Name") { Song.Entities.Outline outline = Business.Do <IOutline>().OutlineBatchAdd(org.Org_ID, obj.Sbj_ID, obj.Cou_ID, column); if (outline != null) { obj.Ol_ID = outline.Ol_ID; } } if (field == "Qus_Explain") { obj.Qus_Explain = column; } //唯一值 obj.Qus_UID = WeiSha.Common.Request.UniqueID(); } //再遍历一遍,取答案 int ansNum = 0; List <Song.Entities.QuesAnswer> ansItem = new List <QuesAnswer>(); foreach (KeyValuePair <String, String> rel in ExcelInput1.DataRelation) { //数据库字段的名称 string field = rel.Value; Match match = new Regex(@"(Ans_Context)(\d+)").Match(field); if (match.Success) { //Excel的列的值 string column = dr[rel.Key].ToString(); if (column == string.Empty || column.Trim() == "") { continue; } Song.Entities.QuesAnswer ans = new Song.Entities.QuesAnswer(); ans.Ans_Context = column; ans.Qus_UID = obj.Qus_UID; ansNum++; ansItem.Add(ans); } } obj.Qus_Title = tranTxt(obj.Qus_Title); int bracketsCount = new Regex(@"([^)]+)").Matches(obj.Qus_Title).Count; //判断是否有错 string error = ""; if (bracketsCount <= 0) { error = "试题中缺少填空项!(填空项用括号标识)"; } if (ansNum <= 0) { error = "缺少答案项"; } if (ansNum < bracketsCount) { error = string.Format("答案项少于填空项;填空项{0}个,答案{1}个", bracketsCount, ansNum); } // obj.Qus_IsError = error != ""; obj.Qus_ErrorInfo = error; if (obj.Sbj_ID == 0) { throw new Exception("当前试题所属专业并不存在"); } if (obj.Cou_ID == 0) { throw new Exception("当前试题所在课程并不存在"); } //if (obj.Ol_ID == 0) throw new Exception("当前试题所在章节并不存在"); if (org != null) { obj.Org_ID = org.Org_ID; } Business.Do <IQuestions>().QuesInput(obj, ansItem); }
/// <summary> /// 将某一行数据加入到数据库 /// </summary> /// <param name="dr"></param> /// <param name="dl"></param> private void _inputData(DataRow dr) { Song.Entities.Questions obj = new Song.Entities.Questions(); obj.Qus_IsUse = true; obj.Qus_Type = this.type; //正确答案 string[] correct = null; //是否有答案 bool isHavAns = false; foreach (KeyValuePair <String, String> rel in ExcelInput1.DataRelation) { //Excel的列的值 string column = dr[rel.Key].ToString(); //数据库字段的名称 string field = rel.Value; if (field == "Qus_ID") { if (column == string.Empty || column.Trim() == "") { continue; } int ques = Convert.ToInt32(column); Song.Entities.Questions isHavObj = Business.Do <IQuestions>().QuesSingle(ques); if (isHavObj != null) { obj = isHavObj; } } //题干难度、专业、试题讲解 if (field == "Qus_Title") { if (column == string.Empty || column.Trim() == "") { return; } obj.Qus_Title = tranTxt(column); } if (field == "Qus_Diff") { obj.Qus_Diff = Convert.ToInt16(column); } if (field == "Sbj_Name") { Song.Entities.Subject subject = Business.Do <ISubject>().SubjectBatchAdd(org.Org_ID, column); if (subject != null) { obj.Sbj_Name = subject.Sbj_Name; obj.Sbj_ID = subject.Sbj_ID; } } if (field == "Cou_Name") { Song.Entities.Course course = Business.Do <ICourse>().CourseBatchAdd(org.Org_ID, obj.Sbj_ID, column); if (course != null) { obj.Cou_ID = course.Cou_ID; } } if (field == "Ol_Name") { Song.Entities.Outline outline = Business.Do <IOutline>().OutlineBatchAdd(org.Org_ID, obj.Sbj_ID, obj.Cou_ID, column); if (outline != null) { obj.Ol_ID = outline.Ol_ID; } } if (field == "Qus_Explain") { obj.Qus_Explain = column; } //唯一值,正确答案,类型 obj.Qus_UID = WeiSha.Common.Request.UniqueID(); if (field == "Ans_IsCorrect") { column = Regex.Replace(column, @"[^1-9]", ","); correct = column.Split(','); } } //再遍历一遍,取答案 List <Song.Entities.QuesAnswer> ansItem = new List <QuesAnswer>(); foreach (KeyValuePair <String, String> rel in ExcelInput1.DataRelation) { //数据库字段的名称 string field = rel.Value; Match match = new Regex(@"(Ans_Context)(\d+)").Match(field); if (match.Success) { //Excel的列的值 string column = dr[rel.Key].ToString(); if (column == string.Empty || column.Trim() == "") { continue; } int index = Convert.ToInt16(match.Groups[2].Value); Song.Entities.QuesAnswer ans = new Song.Entities.QuesAnswer(); ans.Ans_Context = column; foreach (string s in correct) { if (s == string.Empty || s.Trim() == "") { continue; } if (index == Convert.ToInt32(s)) { ans.Ans_IsCorrect = true; isHavAns = true; break; } } ans.Qus_UID = obj.Qus_UID; ansItem.Add(ans); } } if (!isHavAns) { obj.Qus_IsError = true; } //判断是否有错 string error = ""; if (ansItem.Count < 1) { error = "缺少答案选项"; } if (!isHavAns) { error = "没有设置正确答案"; } obj.Qus_IsError = error != ""; obj.Qus_ErrorInfo = error; if (obj.Sbj_ID == 0) { throw new Exception("当前试题所属专业并不存在"); } if (obj.Cou_ID == 0) { throw new Exception("当前试题所在课程并不存在"); } //if (obj.Ol_ID == 0) throw new Exception("当前试题所在章节并不存在"); if (org != null) { obj.Org_ID = org.Org_ID; } Business.Do <IQuestions>().QuesInput(obj, ansItem); }
/// <summary> /// 将某一行数据加入到数据库 /// </summary> /// <param name="dr"></param> /// <param name="dl"></param> private void _inputData(DataRow dr) { Song.Entities.Questions obj = new Song.Entities.Questions(); obj.Qus_IsUse = true; obj.Qus_Type = this.type; //正确答案 int correct = 0; foreach (KeyValuePair <String, String> rel in ExcelInput1.DataRelation) { //Excel的列的值 string column = dr[rel.Key].ToString(); //数据库字段的名称 string field = rel.Value; if (field == "Qus_ID") { if (string.IsNullOrEmpty(column) || column.Trim() == "") { continue; } int ques = Convert.ToInt32(column); Song.Entities.Questions isHavObj = Business.Do <IQuestions>().QuesSingle(ques); if (isHavObj != null) { obj = isHavObj; } } //题干难度、专业、试题讲解 if (field == "Qus_Title") { if (column == string.Empty || column.Trim() == "") { return; } obj.Qus_Title = column; obj.Qus_Title = tranTxt(obj.Qus_Title); } if (field == "Qus_Diff") { obj.Qus_Diff = Convert.ToInt16(column); } if (field == "Sbj_Name") { Song.Entities.Subject subject = Business.Do <ISubject>().SubjectBatchAdd(org.Org_ID, column); if (subject != null) { obj.Sbj_Name = subject.Sbj_Name; obj.Sbj_ID = subject.Sbj_ID; } } if (field == "Cou_Name") { Song.Entities.Course course = Business.Do <ICourse>().CourseBatchAdd(org.Org_ID, obj.Sbj_ID, column); if (course != null) { obj.Cou_ID = course.Cou_ID; } } if (field == "Ol_Name") { Song.Entities.Outline outline = Business.Do <IOutline>().OutlineBatchAdd(org.Org_ID, obj.Sbj_ID, obj.Cou_ID, column); if (outline != null) { obj.Ol_ID = outline.Ol_ID; } } if (field == "Qus_Explain") { obj.Qus_Explain = column; } //唯一值,正确答案,类型 obj.Qus_UID = WeiSha.Common.Request.UniqueID(); if (field == "Ans_IsCorrect") { if (new Regex(@"^\d+$", RegexOptions.Multiline).Match(column).Success) { correct = column == string.Empty ? 0 : Convert.ToInt32(column); } } } //再遍历一遍,取答案 List <Song.Entities.QuesAnswer> ansItem = new List <QuesAnswer>(); foreach (KeyValuePair <String, String> rel in ExcelInput1.DataRelation) { //数据库字段的名称 string field = rel.Value; Match match = new Regex(@"(Ans_Context)(\d+)").Match(field); if (match.Success) { //Excel的列的值 string column = dr[rel.Key].ToString(); if (column == string.Empty || column.Trim() == "") { continue; } int index = Convert.ToInt16(match.Groups[2].Value); Song.Entities.QuesAnswer ans = new Song.Entities.QuesAnswer(); ans.Ans_Context = column; ans.Ans_IsCorrect = index == correct; ans.Qus_UID = obj.Qus_UID; ansItem.Add(ans); } } //判断是否有错 string error = ""; if (ansItem.Count < 1) { error = "缺少答案选项"; } if (correct < 1 || correct > ansItem.Count) { error = string.Format("正确答案的设置不正确,共{0}个答案选项,不能设置为{1}", ansItem.Count, correct); } obj.Qus_IsError = error != ""; obj.Qus_ErrorInfo = error; if (obj.Sbj_ID == 0) { throw new Exception("当前试题所属专业并不存在"); } if (obj.Cou_ID == 0) { throw new Exception("当前试题所在课程并不存在"); } //if (obj.Ol_ID == 0) throw new Exception("当前试题所在章节并不存在"); if (org != null) { obj.Org_ID = org.Org_ID; } Business.Do <IQuestions>().QuesInput(obj, ansItem); }