public StudentModel GetStudent(int studentId) { StudentModel student = null; using (SqlConnection studentConnection = new SqlConnection(connectionString)) { studentConnection.Open(); string command = "SELECT * FROM Student WHERE Id = @studentId;"; SqlCommand selectStudentsCommand = new SqlCommand(command, studentConnection); selectStudentsCommand.Parameters.AddWithValue("@studentId", studentId); SqlDataReader studentReader = selectStudentsCommand.ExecuteReader(); while (studentReader.Read()) { student = new StudentModel(); student.Id = (int)studentReader["Id"]; student.Course = studentReader["Course"].ToString(); student.StudentName = studentReader["StudentName"].ToString(); student.StudentId = (int)studentReader["StudentId"]; student.Year = (int)studentReader["Year"]; if (studentReader["Picture"] != System.DBNull.Value) { student.ConvertByteArrayToImage((byte[])studentReader["Picture"]); } } } return(student); }
public List <StudentModel> GetAllStudents() { List <StudentModel> students = new List <StudentModel>(); using (SqlConnection studentConnection = new SqlConnection(connectionString)) { studentConnection.Open(); string command = "SELECT * FROM Student;"; SqlCommand selectStudentsCommand = new SqlCommand(command, studentConnection); using (SqlDataReader studentReader = selectStudentsCommand.ExecuteReader()) { while (studentReader.Read()) { StudentModel student = new StudentModel(); student.Id = (int)studentReader["Id"]; student.Course = studentReader["Course"].ToString(); student.StudentName = studentReader["StudentName"].ToString(); student.StudentId = (int)studentReader["StudentId"]; student.Year = (int)studentReader["Year"]; if (studentReader["Picture"] != System.DBNull.Value) { student.ConvertByteArrayToImage((byte[])studentReader["Picture"]); } students.Add(student); } } } return(students); }