Advertisement
Guest User

Untitled

a guest
Jul 14th, 2014
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. questions:
  2. question_id (PK, AI), module_id (FK), author_id (FK), approved, question, correct_answer_id (FK)
  3.  
  4. answers:
  5. answer_id (PK, AI), question_id (FK), answer
  6.  
  7. string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
  8. MySqlConnection conn = new MySqlConnection(connStr);
  9.  
  10. string queryUpdateQuestions = "INSERT INTO questions (module_id, author_id, approved, question) VALUES (@module_id, @author_id, @approved, @question)";
  11. MySqlCommand cmdUpdateQuestions = new MySqlCommand(queryUpdateQuestions, conn);
  12. cmdUpdateQuestions.Parameters.Add("@module_id", MySqlDbType.VarChar);
  13. cmdUpdateQuestions.Parameters["@module_id"].Value = ddlModules.SelectedValue.ToString();
  14. cmdUpdateQuestions.Parameters.Add("@author_id", MySqlDbType.VarChar);
  15. cmdUpdateQuestions.Parameters["@author_id"].Value = Session["UserID"].ToString();
  16. cmdUpdateQuestions.Parameters.Add("@approved", MySqlDbType.VarChar);
  17. cmdUpdateQuestions.Parameters["@approved"].Value = 'N';
  18. cmdUpdateQuestions.Parameters.Add("@question", MySqlDbType.VarChar);
  19. cmdUpdateQuestions.Parameters["@question"].Value = txtQuestion.Text;
  20.  
  21. try
  22. {
  23. conn.Open();
  24. cmdUpdateQuestions.ExecuteNonQuery();
  25. }
  26. catch
  27. {
  28. lblError.Text="Unable to add question.";
  29. }
  30. finally
  31. {
  32. conn.Close();
  33. }
  34.  
  35. //????? = get last question_id in 'questions'
  36.  
  37. int a = Convert.ToInt32(ddlNoOfAnswers.SelectedValue.ToString());
  38.  
  39. for (int b=1; b <= a; b++)
  40. {
  41. string queryUpdateAnswers = "INSERT INTO answers (question_id, answer) VALUES (@question_id, @answer)";
  42. MySqlCommand cmdUpdateAnswers = new MySqlCommand(queryUpdateAnswers, conn);
  43. cmdUpdateAnswers.Parameters.Add("@answer", MySqlDbType.VarChar);
  44. cmdUpdateAnswers.Parameters["@answer"].Value = ((TextBox)this.FindControl("txtAnswer" + b)).Text;
  45. cmdUpdateAnswers.Parameters.Add("@question_id", MySqlDbType.VarChar);
  46. cmdUpdateAnswers.Parameters["@question_id"].Value = ?????;
  47.  
  48. try
  49. {
  50. conn.Open();
  51. cmdUpdateAnswers.ExecuteNonQuery();
  52. }
  53. catch
  54. {
  55. lblError.Text="Unable to add answer.";
  56. }
  57. finally
  58. {
  59. conn.Close();
  60. }
  61. }
  62.  
  63. //update 'correct_answer_id' in 'questions'
  64.  
  65. using(MySqlConnection conn = new MySqlConnection(connStr))
  66. {
  67. conn.Open();
  68. using(MySqlTransaction tr = conn.BeginTransaction())
  69. {
  70. ...
  71. // MySqlCommand code goes here
  72. ...
  73. tr.Commit();
  74. }
  75. }
  76.  
  77. string queryUpdateQuestions = @"INSERT INTO questions (.....);
  78. SELECT LAST_INSERT_ID()";
  79.  
  80. using(MySqlCommand cmdUpdateQuestions = new MySqlCommand(queryUpdateQuestions, conn, tr))
  81. {
  82. // build the parameters for the question record
  83. ......
  84.  
  85. // Instead of ExecuteNonQuery, run ExecuteScalar to get back the result of the last SELECT
  86. int lastQuestionID = Convert.ToInt32(cmdUpdateQuestions.ExecuteScalar());
  87.  
  88. ..
  89.  
  90. }
  91.  
  92. string queryUpdateAnswers = @"INSERT INTO answers (question_id, answer)
  93. VALUES (@question_id, @answer);
  94. SELECT LAST_INSERT_ID()";
  95.  
  96. using(MySqlCommand cmdUpdateAnswers = new MySqlCommand(queryUpdateAnswers, conn, tr))
  97. {
  98. // next move the loop inside the using and prepare the parameter before looping to
  99. // to avoid unnecessary rebuild of the parameters and the command
  100. cmdUpdateAnswers.Parameters.Add("@answer", MySqlDbType.VarChar);
  101. cmdUpdateAnswers.Parameters.Add("@question_id", MySqlDbType.VarChar);
  102.  
  103. int lastAnswerID = 0;
  104. // Loop backward so the last answer inserted is the 'correct' one and we could get its ID
  105. for (int b=a; b >= 1; b--)
  106. {
  107. cmdUpdateAnswers.Parameters["@answer"].Value = ((TextBox)this.FindControl("txtAnswer" + b)).Text;
  108. cmdUpdateAnswers.Parameters["@question_id"].Value = lastQuestionID;
  109. lastAnswerID = Convert.ToInt32(cmdUpdateAnswers.ExecuteScalar());
  110. }
  111. ....
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement