public bool Add(Student entity) { bool IsSuccess = false; try { using (SqlConnection con = new SqlConnection(this.sqlconnectionstring)) { con.Open(); SqlCommand cmd = new SqlCommand("INSERT INTO Student(Name,IsActive) VALUES(@Name,@IsActive)", con); cmd.Parameters.AddWithValue("@Name", entity.Name); cmd.Parameters.AddWithValue("@IsActive", entity.IsActive); if (cmd.ExecuteNonQuery() > 0) { IsSuccess = true; } } } catch (Exception) { throw; } return IsSuccess; }
public void Add() { //arrange Boolean isSuccess = true; Student newStudent = new Student(); newStudent.Name = "Harold Paluca"; newStudent.IsActive = false; //act isSuccess = controller.Add(newStudent); //assert Assert.IsTrue(isSuccess); }
public IEnumerable<Student> GetAll() { List<Student> students = new List<Student>(); try { using (SqlConnection con = new SqlConnection(this.sqlconnectionstring)) { con.Open(); SqlCommand cmd = new SqlCommand("SELECT * FROM Student", con); using (SqlDataReader reader = cmd.ExecuteReader()) { if (reader.HasRows) { reader.Read(); Student student = null; student = new Student(); student.ID = Int32.Parse(reader["ID"].ToString()); student.Name = reader["Name"].ToString(); student.IsActive = Boolean.Parse(reader["IsActive"].ToString()); students.Add(student); } } } } catch (Exception) { throw; } return students; }
public bool Update(Student entity) { bool IsSuccess = false; try { using (SqlConnection con = new SqlConnection(this.sqlconnectionstring)) { con.Open(); SqlCommand cmd = new SqlCommand("UPDATE Student SET Name=@Name, IsActive=@IsActive WHERE ID=@ID", con); cmd.Parameters.AddWithValue("@ID", entity.ID); cmd.Parameters.AddWithValue("@Name", entity.Name); cmd.Parameters.AddWithValue("@IsActive", entity.IsActive); if (cmd.ExecuteNonQuery() > 0) { IsSuccess = true; } } } catch (Exception) { throw; } return IsSuccess; }
public Student GetByID(int id) { Student student = null; try { using (SqlConnection con = new SqlConnection(this.sqlconnectionstring)) { con.Open(); SqlCommand cmd = new SqlCommand("SELECT * FROM Student WHERE id=@id",con); cmd.Parameters.AddWithValue("@id", id); using (SqlDataReader reader = cmd.ExecuteReader()) { if (reader.HasRows) { reader.Read(); student = new Student(); student.ID = id; student.Name = reader["Name"].ToString(); student.IsActive = Boolean.Parse(reader["IsActive"].ToString()); } } } } catch (Exception) { throw; } return student; }
public void Update() { //arrange Boolean isSuccess = true; Student newStudent = new Student(); newStudent.ID = 2; newStudent.Name = "Benjie Estrella"; newStudent.IsActive = true; //act isSuccess = controller.Update(newStudent); //assert Assert.IsTrue(isSuccess); }