public IActionResult Put(int DependentID, [FromBody] DependentRequest request) { MySqlConnection conn = new MySqlConnection(_appSettings.ConnectionString); DependentResult result = new DependentResult(); try { conn.Open(); using (MySqlCommand cmd = new MySqlCommand("UPDATE Dependent SET DependentName = @Dependentname, DependentBirth = @DependentBirth, DependentBlood = @DependentBlood, DependentAllergy = @DependentAllergy, DependentSusNo = @DependentSusNo WHERE DependentID = @DependentID", conn)) { cmd.Parameters.AddWithValue("@DependentName", request.DependentName); cmd.Parameters.AddWithValue("@DependentBirth", request.DependentBirth); cmd.Parameters.AddWithValue("@DependentBlood", request.DependentBlood); cmd.Parameters.AddWithValue("@DependentAllergy", request.DependentAllergy); cmd.Parameters.AddWithValue("@DependentSusNo", request.DependentSusNo); cmd.Parameters.AddWithValue("@DependentID", DependentID);//o label em cima tem o nome deste aqui! Tanto faz o nome pois é local cmd.ExecuteNonQuery(); //O que está aqui dentro é só codigo de BD, ou seja, passa os valores para a query! } result.DependentID = DependentID; result.DependentName = request.DependentName; result.DependentBirth = request.DependentBirth; result.DependentBlood = request.DependentBlood; result.DependentAllergy = request.DependentAllergy; result.DependentSusNo = request.DependentSusNo; return(new OkObjectResult(result)); } catch (Exception ex) { return(BadRequest(ex)); } finally { conn.Dispose(); conn.Close(); } }
public IActionResult PostDependent([FromBody] DependentRequest request) { MySqlConnection conn = new MySqlConnection(_appSettings.ConnectionString); int DependentID = 0; try { conn.Open(); using (MySqlCommand cmd = new MySqlCommand("INSERT INTO Dependent (DependentName, DependentBirth, DependentBlood, DependentAllergy, DependentSusNo, UserID) VALUES(@DependentName, @DependentBirth, @DependentBlood, @DependentAllergy, @DependentSusNo, @UserID)", conn)) { cmd.Parameters.AddWithValue("@DependentName", request.DependentName); cmd.Parameters.AddWithValue("@DependentBirth", request.DependentBirth); cmd.Parameters.AddWithValue("@DependentBlood", request.DependentBlood); cmd.Parameters.AddWithValue("@DependentAllergy", request.DependentAllergy); cmd.Parameters.AddWithValue("@DependentSusNo", request.DependentSusNo); cmd.Parameters.AddWithValue("@UserID", request.UserID); cmd.ExecuteNonQuery(); using (MySqlCommand cmd2 = new MySqlCommand("SELECT last_insert_id()", conn)) { DependentID = (int)(ulong)cmd2.ExecuteScalar(); } } return(new OkObjectResult(new DependentResult { DependentID = DependentID, DependentName = request.DependentName, DependentBirth = request.DependentBirth, DependentBlood = request.DependentBlood, DependentAllergy = request.DependentAllergy, DependentSusNo = request.DependentSusNo, UserID = request.UserID })); } catch (Exception ex) { return(BadRequest(ex)); } finally { conn.Dispose(); conn.Close(); } }