//search for units using all the words the user has as input public Unit[] SearchUnits(string searchInput) { List<Unit> units = new List<Unit>(); //split the search input into arrays of words string[] words = searchInput.Split(' '); //replace every space with a @ symbol for (int i = 0; i < words.Length; i++) { if (words[i]=="") words[i] = "@"; } string sql = "SELECT `unit_id`, `unit_name`, `unit_description` FROM `units` WHERE `unit_name` LIKE '%" + words[0] + "%'"; //loop throughout all the words for (int i = 1; i < words.Length; i++) { sql = sql + " OR `unit_name` LIKE '%" + words[i] + "%'"; } con = new MySqlConnection(connectionString); cmd = new MySqlCommand(sql, con); try { con.Open();//open connection dr = cmd.ExecuteReader(); while (dr.Read()) { Unit u = new Unit((int)dr["unit_id"], dr["unit_name"].ToString(), dr["unit_description"].ToString()); units.Add(u); } } catch (MySqlException ex)// incase of errors catch them here { throw new Exception(ex.Message); } finally { con.Close(); } return units.ToArray(); }
//loads all the units from the units table public Unit[] LoadUnits() { List<Unit> units = new List<Unit>(); string sql = "SELECT `unit_id`, `unit_name`, `unit_description` FROM `units` WHERE 1"; con = new MySqlConnection(connectionString); cmd = new MySqlCommand(sql, con); try { con.Open();//open connection dr = cmd.ExecuteReader(); while (dr.Read()) { Unit u = new Unit((int)dr["unit_id"], dr["unit_name"].ToString(), dr["unit_description"].ToString()); units.Add(u); } } catch (MySqlException ex)// incase of errors catch them here { throw new Exception(ex.Message); } finally { con.Close(); } return units.ToArray(); }