public IActionResult Get(int AttendantID) { MySqlConnection conn = new MySqlConnection(_appSettings.ConnectionString); AttendantResult result = new AttendantResult(); try { conn.Open(); using (MySqlCommand cmd = new MySqlCommand("SELECT AttendantID, AttendantName, AttendantCpf, AttendantEmail FROM Attendant WHERE AttendantID =" + AttendantID, conn)) { MySqlDataReader dataReader = cmd.ExecuteReader(); while (dataReader.Read()) { result.AttendantID = dataReader.GetInt32(0); result.AttendantName = dataReader.GetString(1); result.AttendantCpf = dataReader.GetString(2); result.AttendantEmail = dataReader.GetString(3); } } return(new OkObjectResult(result)); } catch (Exception ex) { return(BadRequest(ex)); } finally { conn.Dispose(); conn.Close(); } }
public IActionResult Post([FromBody] AttendantRequest request) { MySqlConnection conn = new MySqlConnection(_appSettings.ConnectionString); AttendantResult result = new AttendantResult(); try { conn.Open(); using (MySqlCommand cmd = new MySqlCommand("INSERT INTO Attendant (AttendantName, AttendantCpf, AttendantEmail, AttendantPass, UbsID) VALUES(@AttendantName, @AttendantCpf, @AttendantEmail, @AttendantPass, @UbsID)", conn)) { cmd.Parameters.AddWithValue("@AttendantName", request.AttendantName); cmd.Parameters.AddWithValue("@AttendantCpf", request.AttendantCpf); cmd.Parameters.AddWithValue("@AttendantEmail", request.AttendantEmail); cmd.Parameters.AddWithValue("@AttendantPass", request.AttendantPass); cmd.Parameters.AddWithValue("@UbsID", request.UbsID); cmd.ExecuteNonQuery(); using (MySqlCommand cmd2 = new MySqlCommand("SELECT last_insert_id()", conn)) { result.AttendantID = (int)(ulong)cmd2.ExecuteScalar(); } } return(new OkObjectResult(result)); } catch (Exception ex) { return(BadRequest(ex)); } finally { conn.Dispose(); conn.Close(); } }