public string AddQuestionTotest(long testInfoId, string datas) { string[] ids = datas.Split('#'); test t = new test(); t.test_TestInfoId = testInfoId; string s = "ok"; for (int i = 0; i < ids.Length; i++) { long questionId = Convert.ToInt64(ids[i]); t.test_QuestionId = questionId; Test T = new Test(); long count = T.SearchCount(u => u.test_QuestionId == questionId && u.test_TestInfoId == testInfoId); if (count > 0) { s = "您所选问题中含有问卷中已存在问题,已为您自动跳过。"; continue; } try { T.Add(t); } catch { return("添加错误,请重试!"); } } return(s); }
public string ImportQusetions(string filename, string id) { long testInfoId = 0; try { testInfoId = new TestInfo().Search(u => u.testInfo_OwnComPanyId == id && u.testInfo_IsDel == false)[0].testInfo_Id; } catch { return("您还没有创建问卷!"); } #region 打开文件,检查数据格式并转化为DataTable string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + ";Extended Properties=Excel 8.0;"; OleDbConnection conn = new OleDbConnection(strConn); conn.Open(); OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]", conn); DataSet ds = new DataSet(); da.Fill(ds, "table"); conn.Close(); DataTable table = ds.Tables["table"]; string result = CheckDataTable(table); if (result != "ok") { return(result); } #endregion int errorTests = 0; int errorQuestions = 0; Question Q = new Question(); Test T = new Test(); //遍历每行,将数据包装进rightTests和rightQuestions集合 test t = new test(); question q = new question(); for (int i = 0; i < table.Rows.Count; i++) { bool AllNull = string.IsNullOrEmpty(table.Rows[i][2].ToString()) && string.IsNullOrEmpty(table.Rows[i][3].ToString()) && string.IsNullOrEmpty(table.Rows[i][4].ToString()) && string.IsNullOrEmpty(table.Rows[i][5].ToString()) && string.IsNullOrEmpty(table.Rows[i][6].ToString()) && string.IsNullOrEmpty(table.Rows[i][7].ToString()); if (AllNull) { q.question_Option1 = "1"; q.question_Option2 = "2"; q.question_Option3 = "3"; q.question_Option4 = "4"; q.question_Option5 = "5"; q.question_Option6 = "6"; q.question_Option7 = "7"; } else { q.question_Option1 = table.Rows[i][2].ToString(); q.question_Option2 = table.Rows[i][3].ToString(); q.question_Option3 = table.Rows[i][4].ToString(); q.question_Option4 = table.Rows[i][5].ToString(); q.question_Option5 = table.Rows[i][6].ToString(); q.question_Option6 = table.Rows[i][7].ToString(); q.question_Option7 = table.Rows[i][8].ToString(); } string stem = table.Rows[i][0].ToString(); List <question> questions = Q.Search(k => k.qusetion_Stem == stem && k.question_IsDel == false); if (questions.Count > 0) {//题库中已存在此问题,不再插入,直接引用 t.test_QuestionId = questions[0].question_Id; t.test_TestInfoId = testInfoId; try { long qid = t.test_QuestionId; if (T.SearchCount(k => k.test_QuestionId == qid && k.test_TestInfoId == testInfoId && k.question.question_IsDel == false) > 0) { continue; } T.Add(t); } catch { errorTests++; } } else { q.qusetion_Stem = stem; string myclass = table.Rows[i][1].ToString(); List <@class> classes = new _Class().Search(k => k.class_Class == myclass); if (classes.Count <= 0) { return("导入中止,请确定您所插入表中的问题类别在类别中已存在!<br/>请在题库类别管理中添加。"); } else { q.question_ClassId = classes[0].class_Id; } try { Q.Add(q); } catch { errorQuestions++; } t.test_QuestionId = q.question_Id; t.test_TestInfoId = testInfoId; try { T.Add(t); } catch { errorTests++; } } } if (errorTests == 0 && errorQuestions == 0) { return("ok"); } else { return("有" + errorQuestions + "条数据出错,请检查数据!"); } }