コード例 #1
0
        public async Task<object> InsertQuestionAnswer(Questionare_question_form qdata)
        {
            DBConnector d = new DBConnector();
            if (!d.SQLConnect())
                return WebApiApplication.CONNECTDBERRSTRING;

            string insertintoquestionareresobj = string.Format("insert into {0} values ",Questionare_result_obj.FieldName.TABLE_NAME);
            
            foreach(Questionare_question_answer item in qdata.question_list)
            {
                insertintoquestionareresobj += string.Format("({0},{1})", item.questionare_question_id, item.answer);
                if (item != qdata.question_list.Last())
                    insertintoquestionareresobj += ",";
            }

            string insertintoquestionareressub = string.Format("insert into {0} values ({1},'{2}')",
                Questionare_result_sub.FieldName.TABLE_NAME, qdata.question_list.First().questionare_set_id,
                qdata.suggestion);

            string truecasecmd = string.Format("BEGIN {0} {1} END", insertintoquestionareresobj, insertintoquestionareressub);
            d.iCommand.CommandText = string.Format("if exists (select * from {0} where {1} = {2}) {3} else return ",
                Questionare_set.FieldName.TABLE_NAME, Questionare_set.FieldName.QUESTIONARE_SET_ID, qdata.question_list.First().questionare_set_id,
                truecasecmd);
            try
            {
                int rowaffacted = await d.iCommand.ExecuteNonQueryAsync();
                if (rowaffacted > 0)
                    return null;
                else
                    return "แบบสอบถามนี้ถูกลบแล้ว";
            }
            catch (Exception ex)
            {
                //Handle error from sql execution
                return ex.Message;
            }
            finally
            {
                //Whether it success or not it must close connection in order to end block
                d.SQLDisconnect();
            }
        }
コード例 #2
0
        public async Task<IHttpActionResult> PutForAnswerQuestions(List<object> list)
        {
            Questionare_question_form q = new Questionare_question_form();
            q.suggestion = JObject.Parse(list.Last().ToString())["suggestion"].ToString();
            
            //add answer for each question to pre-last item (pre-last item is suggestion)
            foreach(JObject item in list)
            {
                if (item == list.Last()) break;
                q.question_list.Add(new Questionare_question_answer
                {
                    questionare_set_id = Convert.ToInt32(item["questionare_set_id"]),
                    questionare_question_id = Convert.ToInt32(item["questionare_question_id"]),
                    answer = Convert.ToInt32(item["answer"])
                });
            }

            object result = await datacontext.InsertQuestionAnswer(q);
            if (result == null)
                return Ok();
            else
                return BadRequest(result.ToString());
        }