public List <Labourer> getLabourers(MySqlConnection conn) { string sql = "SELECT * FROM labourer"; MySqlCommand sqlComm = new MySqlCommand(sql, conn); MySqlDataReader reader = sqlComm.ExecuteReader(); if (reader.HasRows) { List <Labourer> labourersList = new List <Labourer>(); while (reader.Read()) { int id = reader.GetInt32("id"); string name = reader.GetString("name"); int age = reader.GetInt32("age"); string gender = reader.GetString("gender"); Labourer labourer = new Labourer(); labourer.Id = id; labourer.Name = name; labourer.Age = age; labourer.Gender = gender; labourersList.Add(labourer); } return(labourersList); } else { return(null); } }
public int addNewLabourer(MySqlConnection conn, Labourer labourer) { string sql = "INSERT INTO labourer (name, age, gender) " + "VALUES ('" + labourer.Name + "', " + labourer.Age + " , '" + labourer.Gender + "')"; MySqlCommand sqlComm = new MySqlCommand(sql, conn); return(sqlComm.ExecuteNonQuery()); }
private void submit_labourer_btn_Click(object sender, EventArgs e) { DbConnector dbcon = new DbConnector(); dbcon.connect(); string labourName = nameTextBox.Text; DateTime labourDob = dob_datePicker.Value; int labourAge = DateTime.Now.Year - labourDob.Year; string labourGender = (male_radioBtn.Checked ? "MALE" : "FEMALE"); Labourer labourer = new Labourer(); labourer.Name = labourName; labourer.Age = labourAge; labourer.Gender = labourGender; LabourerHandler labourerHandler = new LabourerHandler(); int recCount = labourerHandler.addNewLabourer(dbcon.getConn(), labourer); MessageBox.Show(recCount + " record(s) has been inserted !"); addLabour_panel.Visible = false; }