/// <summary> /// Saves the import data. /// </summary> /// <param name="importZipCodes">The import zip codes.</param> /// <exception cref="System.NotImplementedException"></exception> public void SaveImportData(IEnumerable <OutboundData> importZipCodes) { try { //先砍掉全部資料 using (var db = new TestDataEntities()) { foreach (var item in db.OutboundData.OrderBy(x => x.ID)) { db.OutboundData.Remove(item); } db.SaveChanges(); } //再把匯入的資料給存到資料庫 using (var db = new TestDataEntities()) { foreach (var item in importZipCodes) { db.OutboundData.Add(item); } db.SaveChanges(); } } catch (Exception ex) { throw; } }
protected void Button1_Click(object sender, EventArgs e) { string sname = TextBox1.Text; using (var context = new TestDataEntities()) { var user = (from u in context.Scores where u.Name == sname select u.Name).FirstOrDefault(); if (user != null) { Session["Username"] = Label1.Text; Response.Redirect("Exam.aspx"); } else { Score s = new Score { Name = sname, Marks = 0, }; context.Scores.Add(s); context.SaveChanges(); Session["Username"] = Label1.Text; Response.Redirect("Exam.aspx"); } } }
private AnswerStatus CheckAnswer() { var yourAnswer = RadioButtonList1.SelectedValue; using (var context = new TestDataEntities()) { var question = (from q in context.Questions where q.QuizId == 1 select q).FirstOrDefault(); if (yourAnswer == null) { return(AnswerStatus.Blank); } return(question.Ans == yourAnswer ? AnswerStatus.Correct : AnswerStatus.Wrong); } }
protected void Button1_Click(object sender, EventArgs e) { using (var context = new TestDataEntities()) { Question q = new Question { QuizId = 10, Quest = TextBox1.Text, Answers = TextBox2.Text, Ans = TextBox3.Text, Level = Convert.ToInt32(TextBox4.Text) }; context.Questions.Add(q); context.SaveChanges(); } }
protected void Button2_Click(object sender, EventArgs e) { switch (CheckAnswer()) { case AnswerStatus.Correct: Level = Level + 1; break; case AnswerStatus.Wrong: if (Level > 0) { Level = Level - 1; } break; } QuizIds.Add(int.Parse(QuizIdField.Value)); RadioButtonList1.Items.Clear(); using (var context = new TestDataEntities()) { var question = (from q in context.Questions where !QuizIds.Contains(q.QuizId) && // QuizIds = {1} ==> 'QuizId NOT IN (1)' q.Level == Level select q).FirstOrDefault(); if (question != null) { Label1.Text = question.Quest; string[] options = question.Answers.Split('|'); foreach (string option in options) { ListItem i = new ListItem(option); RadioButtonList1.Items.Add(i); } } } }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { using (var context = new TestDataEntities()) { var question = (from q in context.Questions where q.Level == 0 select q).FirstOrDefault(); if (question != null) { Label1.Text = question.Quest; QuizIdField.Value = question.QuizId.ToString(); string[] options = question.Answers.Split('|'); foreach (string option in options) { ListItem i = new ListItem(option); RadioButtonList1.Items.Add(i); } } } } }