public static List <DepartmentClass> FindById(int id) { List <DepartmentClass> currentDepartment = new List <DepartmentClass> { }; MySqlConnection conn = DB.Connection(); conn.Open(); MySqlCommand cmd = conn.CreateCommand() as MySqlCommand; cmd.CommandText = @"SELECT * FROM departments WHERE id = " + id + ";"; MySqlDataReader rdr = cmd.ExecuteReader() as MySqlDataReader; while (rdr.Read()) { int idz = rdr.GetInt32(0); string name = rdr.GetString(1); DepartmentClass newDepartment = new DepartmentClass(name, idz); currentDepartment.Add(newDepartment); } conn.Close(); if (conn != null) { conn.Dispose(); } return(currentDepartment); }
public static List <DepartmentClass> GetAll() { List <DepartmentClass> allDepartments = new List <DepartmentClass> { }; MySqlConnection conn = DB.Connection(); conn.Open(); var cmd = conn.CreateCommand() as MySqlCommand; cmd.CommandText = @"SELECT * FROM departments;"; MySqlDataReader rdr = cmd.ExecuteReader() as MySqlDataReader; while (rdr.Read()) { int id = rdr.GetInt32(0); string name = rdr.GetString(1); DepartmentClass newDepartment = new DepartmentClass(name, id); allDepartments.Add(newDepartment); } conn.Close(); if (conn != null) { conn.Dispose(); } return(allDepartments); }