/// <summary> /// Constructor Get Data from connected database /// </summary> /// <param name="id"></param> public List <HumanModel> HumanListModel(int arg_id) { Get_Connection(); ID = arg_id; List <HumanModel> res_data = new List <HumanModel>(); try { MySqlCommand cmd = new MySqlCommand(); cmd.Connection = connection; cmd.CommandText = string.Format("SELECT * FROM human;"); try { MySqlDataReader reader = cmd.ExecuteReader(); var dataTable = new DataTable(); dataTable.Load(reader); if (dataTable != null) //confirm data is available { if (dataTable.Rows.Count > 0) //check row count { foreach (DataRow dr in dataTable.Rows) { HumanModel data = new HumanModel(); data.ID = int.Parse(dr["ID"].ToString()); data.Name = dr["NAME"].ToString(); res_data.Add(data); } } } } catch (MySqlException e) { string MessageString = "Read error occurred / entry not found loading the Column details: " + e.ErrorCode + " - " + e.Message + "; \n\nPlease Continue"; Name = MessageString; //Address1 = Address2 = null; } } catch (MySqlException e) { string MessageString = "The following error occurred loading the Column details: " + e.ErrorCode + " - " + e.Message; Name = MessageString; // Address1 = Address2 = null; } connection.Close(); return(res_data); }
public List <HumanModel> getSomeone(string name, int ageSt, int ageEd) { List <HumanModel> someone = new List <HumanModel>(); // string str_sql = "SELECT * FROM human where NAME like '%" + name + "%'" + " AND Age BETWEEN " + ageSt + " AND " + ageEd + ";"; try { MySqlCommand cmd = new MySqlCommand(); Get_Connection(); cmd.Connection = connection; cmd.CommandText = str_sql; DataTable dt = new DataTable(); dt.Load(cmd.ExecuteReader()); if (dt != null) //confirm data is available { if (dt.Rows.Count > 0) //check row count { foreach (DataRow dr in dt.Rows) { HumanModel data = new HumanModel(); data.ID = int.Parse(dr["ID"].ToString()); data.Name = dr["NAME"].ToString(); someone.Add(data); } } } } catch (Exception e) { throw e; } finally { if (connection.State == ConnectionState.Open) { connection.Close(); } } // return(someone); }