Advertisement
Guest User

StudentController.cs

a guest
Jan 20th, 2020
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.34 KB | None | 0 0
  1. using StudentMvc.Models;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Web.Mvc;
  7.  
  8. namespace StudentMvc.Controllers
  9. {
  10. public class StudentController : Controller
  11. {
  12. private readonly string mvcDbConnectionString = @"Server=DESKTOP-H8GBH2T\SQLEXPRESS;Database=StudentMvc;Trusted_Connection=True;";
  13.  
  14. public ActionResult Index()
  15. {
  16. List<Student> itemsList = new List<Student>();
  17. using (SqlConnection conn = new SqlConnection(mvcDbConnectionString))
  18. {
  19. string sql = @"SELECT * FROM dbo.Student;";
  20.  
  21. DataTable dt = new DataTable();
  22. SqlDataAdapter da = new SqlDataAdapter();
  23. da.SelectCommand = new SqlCommand(sql, conn);
  24.  
  25. da.Fill(dt);
  26.  
  27. foreach (DataRow row in dt.Rows)
  28. {
  29. var item = new Student();
  30. item.Id = int.Parse(row["Id"].ToString());
  31. item.LastName = row["LastName"].ToString();
  32. item.FirstName = row["FirstName"].ToString();
  33. item.City = row["City"].ToString();
  34. if(!String.IsNullOrEmpty(row["Age"].ToString()))
  35. {
  36. item.Age = int.Parse(row["Age"].ToString());
  37. }
  38. itemsList.Add(item);
  39. }
  40. }
  41. return View(itemsList);
  42. }
  43.  
  44. public ActionResult Create(Student model)
  45. {
  46. using (SqlConnection conn = new SqlConnection(mvcDbConnectionString))//uspostavi vezu
  47. {
  48. using (SqlCommand cmd = new SqlCommand())//definiraj naredbu koju ces poslat
  49. {
  50.  
  51. cmd.Connection = conn;//definiraj vezu koju ce naredba koristiti za izvrsavanje
  52. cmd.CommandType = CommandType.Text;//tip naredbe(ja msm da je uvijek text kada saljes sql upite)
  53. cmd.CommandText = "INSERT INTO dbo.Student(firstName, lastName, city, age) VALUES(@firstName, @lastName, @city, @age)";//sql upit
  54.  
  55.  
  56.  
  57. cmd.Parameters.AddWithValue("@firstName", model.FirstName);//ovako definiramo parametre
  58. cmd.Parameters.AddWithValue("@lastName", model.LastName);
  59. cmd.Parameters.AddWithValue("@city", model.City);
  60. cmd.Parameters.AddWithValue("@age", model.Age);
  61.  
  62. try
  63. {
  64. conn.Open();//otvorimo vezu
  65. cmd.ExecuteNonQuery();//zapocnemo izvrsavanje naredbe na vezi
  66. return RedirectToAction("Index");
  67. }
  68. catch (SqlException e)
  69. {
  70. return View();
  71. }
  72. }
  73. }
  74. }
  75.  
  76. public ActionResult Delete(int studentId)
  77. {
  78. using (SqlConnection conn = new SqlConnection(mvcDbConnectionString))
  79. {
  80. using (SqlCommand cmd = new SqlCommand())
  81. {
  82. cmd.Connection = conn;
  83. cmd.CommandType = CommandType.Text;
  84. cmd.CommandText = "DELETE FROM dbo.Student WHERE Id = @id";
  85.  
  86. cmd.Parameters.AddWithValue("@id", studentId);
  87.  
  88. try
  89. {
  90. conn.Open();
  91. cmd.ExecuteNonQuery();
  92. return RedirectToAction("Index");
  93. }
  94. catch (SqlException e)
  95. {
  96. return View();
  97. }
  98. }
  99. }
  100. }
  101.  
  102. public ActionResult Edit(int studentId)
  103. {
  104. Student student = new Student();
  105. using (SqlConnection conn = new SqlConnection(mvcDbConnectionString))
  106. {
  107. SqlCommand command = new SqlCommand("SELECT * FROM dbo.Student WHERE Id = @id;", conn);
  108. command.Parameters.AddWithValue("@id", studentId);
  109. conn.Open();
  110. using (SqlDataReader reader = command.ExecuteReader())//da procita select sql-a
  111. {
  112. if (reader.Read())//citanje
  113. {
  114. student.Id = int.Parse(reader["Id"].ToString());//cita od stupca id
  115. student.FirstName = reader["FirstName"].ToString();
  116. student.LastName = reader["LastName"].ToString();
  117. student.City = reader["City"].ToString();
  118. if (!String.IsNullOrEmpty(reader["Age"].ToString()))
  119. {
  120. student.Age = int.Parse(reader["Age"].ToString());
  121. }
  122. return View(student);
  123. }
  124. }
  125. return View();
  126. }
  127. }
  128.  
  129. [HttpPost]
  130. public ActionResult Edit(Student student)
  131. {
  132. using (SqlConnection conn = new SqlConnection(mvcDbConnectionString))
  133. {
  134. using (SqlCommand cmd = new SqlCommand())
  135. {
  136. cmd.Connection = conn;
  137. cmd.CommandType = CommandType.Text;
  138. cmd.CommandText = "UPDATE dbo.Student SET FirstName = @firstName, LastName = @lastName, Age = @age, City = @city WHERE Id = @id";
  139.  
  140. cmd.Parameters.AddWithValue("@id", student.Id);
  141. cmd.Parameters.AddWithValue("@firstName", student.FirstName);
  142. cmd.Parameters.AddWithValue("@lastName", student.LastName);
  143. cmd.Parameters.AddWithValue("@age", student.Age);
  144. cmd.Parameters.AddWithValue("@city", student.City);
  145.  
  146. try
  147. {
  148. conn.Open();
  149. cmd.ExecuteNonQuery();
  150. return RedirectToAction("Index");
  151. }
  152. catch (SqlException e)
  153. {
  154. return View();
  155. }
  156. }
  157. }
  158. }
  159. }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement