/// <summary> /// 更新一条数据 /// </summary> public bool Update(questionModel model) { StringBuilder strSql = new StringBuilder(); strSql.Append("update question set "); strSql.Append("nvc_qcontent=@nvc_qcontent,"); strSql.Append("int_qtype=@int_qtype,"); strSql.Append("nvc_source=@nvc_source"); strSql.Append(" where nc_qid=@nc_qid "); SqlParameter[] parameters = { new SqlParameter("@nvc_qcontent", SqlDbType.NVarChar, 2000), new SqlParameter("@int_qtype", SqlDbType.Int, 4), new SqlParameter("@nvc_source", SqlDbType.NVarChar, 500), new SqlParameter("@nc_qid", SqlDbType.NChar, 10) }; parameters[0].Value = model.nvc_qcontent; parameters[1].Value = model.int_qtype; parameters[2].Value = model.nvc_source; parameters[3].Value = model.nc_qid; int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters); if (rows > 0) { return(true); } else { return(false); } }
/// <summary> /// 增加一条数据 /// </summary> public bool Add(questionModel model) { StringBuilder strSql = new StringBuilder(); strSql.Append("insert into question("); strSql.Append("nc_qid,nvc_qcontent,int_qtype,nvc_source)"); strSql.Append(" values ("); strSql.Append("@nc_qid,@nvc_qcontent,@int_qtype,@nvc_source)"); SqlParameter[] parameters = { new SqlParameter("@nc_qid", SqlDbType.NChar, 10), new SqlParameter("@nvc_qcontent", SqlDbType.NVarChar, 2000), new SqlParameter("@int_qtype", SqlDbType.Int, 4), new SqlParameter("@nvc_source", SqlDbType.NVarChar, 500) }; parameters[0].Value = model.nc_qid; parameters[1].Value = model.nvc_qcontent; parameters[2].Value = model.int_qtype; parameters[3].Value = model.nvc_source; int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters); if (rows > 0) { return(true); } else { return(false); } }
/// <summary> /// 得到一个对象实体 /// </summary> public questionModel DataRowToModel(DataRow row) { questionModel model = new questionModel(); if (row != null) { if (row["nc_qid"] != null) { model.nc_qid = row["nc_qid"].ToString(); } if (row["nvc_qcontent"] != null) { model.nvc_qcontent = row["nvc_qcontent"].ToString(); } if (row["int_qtype"] != null && row["int_qtype"].ToString() != "") { model.int_qtype = int.Parse(row["int_qtype"].ToString()); } if (row["nvc_source"] != null) { model.nvc_source = row["nvc_source"].ToString(); } } return(model); }
public IHttpActionResult Put(questionModel questionParameter) { try { int ChoiceID; tbl_question DBquestion = db.tbl_question.Find(questionParameter.ID); DBquestion.question = questionParameter.question; DBquestion.question = questionParameter.question; DBquestion.image_url = questionParameter.image_url; DBquestion.thumb_url = questionParameter.thumb_url; //Assuming field published_at is updated when the record is edited DBquestion.published_at = DateTime.Now; db.SaveChanges(); //Since this approach allows to add N answers to the question it is preferable to delete the old ones and insert the new ones. DBquestion.rel_question_choices.Clear(); //for all the answers/choices... foreach (choices choices in questionParameter.Choices) { //if the choice doesn't exist on the answers table, it is added if (!db.tbl_choices.Any(m => m.choice == choices.choice)) { tbl_choices DBchoices = new tbl_choices(); DBchoices.choice = choices.choice; db.tbl_choices.Add(DBchoices); db.SaveChanges(); ChoiceID = DBchoices.ID; } //otherwise get the id else { ChoiceID = db.tbl_choices.Where(m => m.choice == choices.choice).Select(m => m.ID).FirstOrDefault(); } //and the answer and its votation is added in the relational table related to the question db.rel_question_choices.Add(new rel_question_choices { questionID = DBquestion.ID, choiceID = ChoiceID, votes = choices.votes }); db.SaveChanges(); } return(Ok()); } catch (Exception e) { return(InternalServerError(new Exception(e.Message))); } }
public IHttpActionResult questions(questionModel questionParameter) { try { int ChoiceID; tbl_question DBquestion = new tbl_question(); DBquestion.question = questionParameter.question; DBquestion.image_url = questionParameter.image_url; DBquestion.thumb_url = questionParameter.thumb_url; DBquestion.published_at = DateTime.Now; //The question is added to the DB db.tbl_question.Add(DBquestion); db.SaveChanges(); //for all the answers/choices... foreach (choices choices in questionParameter.Choices) { //if the choice doesn't exist on the answers table, it is added if (!db.tbl_choices.Any(m => m.choice == choices.choice)) { tbl_choices DBchoices = new tbl_choices(); DBchoices.choice = choices.choice; db.tbl_choices.Add(DBchoices); db.SaveChanges(); ChoiceID = DBchoices.ID; } //otherwise get the id else { ChoiceID = db.tbl_choices.Where(m => m.choice == choices.choice).Select(m => m.ID).FirstOrDefault(); } //and the answer and its votation is added in the relational table related to the question db.rel_question_choices.Add(new rel_question_choices { questionID = DBquestion.ID, choiceID = ChoiceID, votes = choices.votes }); db.SaveChanges(); } return(Ok()); } catch (Exception e) { return(InternalServerError(new Exception(e.Message))); } }
public ActionResult Edit(int id, questionModel q) { try { var qid = id; question qq = new question(); qq = qts.GetById(id); qq.question_name = q.question_name; qq.question_description = q.question_description; qq.id_Question = q.id_Question; qq.quiz_id_Quiz = q.quiz_id_Quiz; qts.Commit(); return(RedirectToAction("Index", new { id = qid })); } catch { return(View()); } }
// GET: QuestionAnswers/Create public ActionResult loadQuiz(int id) { QuizQuestionAnswers qqa = new QuizQuestionAnswers(); qqa.quizId = id; quiz quiz = qz.GetById(id); var questions = qts.GetMany().Where(x => x.quiz_id_Quiz.Equals(quiz.id_Quiz)); qqa.Questions = new List <questionModel>(); qqa.answers = new List <answerModel>(); foreach (question quest in questions) { questionModel questionModel = new questionModel(); questionModel.id_Question = quest.id_Question; questionModel.question_name = quest.question_name; questionModel.question_description = quest.question_description; questionModel.userchoice = null; var answers = ans.GetMany().Where(x => x.question_id_Question.Equals(quest.id_Question)); foreach (answer answ in answers) { answerModel answerModel = new answerModel(); answerModel.id_Answer = answ.id_Answer; answerModel.question_id_Question = answ.question_id_Question; answerModel.answer_name = answ.answer_name; answerModel.flag = answ.flag; qqa.answers.Add(answerModel); } qqa.Questions.Add(questionModel); } return(View(qqa)); }
public ActionResult Create(questionModel qm) { try { // TODO: Add insert logic here question q = new question(); q.question_name = qm.question_name; q.question_description = qm.question_description; q.quiz_id_Quiz = qm.quiz_id_Quiz; q.id_Question = qm.id_Question; qts.Add(q); qts.Commit(); return(RedirectToAction("Index", new { id = q.quiz_id_Quiz })); } catch { return(View()); } }
public IHttpActionResult questions(int question_id) { try { //get the question questionModel question = db.tbl_question.Select(m => new questionModel { ID = m.ID, question = m.question, image_url = m.image_url, thumb_url = m.thumb_url, published_at = m.published_at }).Where(m => m.ID == question_id).FirstOrDefault(); //get the related choices var choiceslist = db.rel_question_choices.Where(m => m.questionID == question_id); //inserts its answers according to the model structure question.Choices = new List <choices>(); foreach (rel_question_choices choices in choiceslist) { if (choices.questionID == question.ID) { question.Choices.Add(new choices { choice = choices.tbl_choices.choice, votes = choices.votes }); } } return(Ok(question)); } catch (Exception e) { return(InternalServerError(new Exception(e.Message))); } }
/// <summary> /// 得到一个对象实体 /// </summary> public questionModel GetModel(string nc_qid) { StringBuilder strSql = new StringBuilder(); strSql.Append("select top 1 nc_qid,nvc_qcontent,int_qtype,nvc_source from question "); strSql.Append(" where nc_qid=@nc_qid "); SqlParameter[] parameters = { new SqlParameter("@nc_qid", SqlDbType.NChar, 10) }; parameters[0].Value = nc_qid; questionModel model = new questionModel(); DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters); if (ds.Tables[0].Rows.Count > 0) { return(DataRowToModel(ds.Tables[0].Rows[0])); } else { return(null); } }