public IActionResult GetHistoric(int id) { string sql = $"SELECT * from historicoGoles WHERE id = {id}"; HistoricoGoles historicoGoles = new HistoricoGoles(); try { using (SqlConnection cnn = new SqlConnection(AfaDB.cnnString)) { using (SqlCommand cmd = new SqlCommand(sql, cnn)) { SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { historicoGoles.id = dr.GetFieldValue <int>(dr.GetOrdinal("id")); historicoGoles.jugadorId = dr.GetFieldValue <int>(dr.GetOrdinal("jugadorId")); historicoGoles.cantGoles = dr.GetFieldValue <int>(dr.GetOrdinal("cantGoles")); } } return(new OkObjectResult(historicoGoles)); } } catch (Exception ex) { Console.WriteLine(ex.StackTrace); return(new NotFoundResult()); } }
public IActionResult Post(HistoricoGoles historicoGoles) { string sql = $"INSERT INTO historicoGoles (id, jugadorId, cantGoles)"; sql += "VALUES(@id, @jugadorId, @cantGoles)"; try { using (SqlConnection cnn = new SqlConnection(AfaDB.cnnString)) { using (SqlTransaction trn = cnn.BeginTransaction()) { try { using (SqlCommand cmd = new SqlCommand(sql, cnn)) { cmd.Transaction = trn; cmd.Parameters.Add(new SqlParameter("@id", historicoGoles.id)); cmd.Parameters.Add(new SqlParameter("@nombre", historicoGoles.jugadorId)); cmd.Parameters.Add(new SqlParameter("@ciudad", historicoGoles.cantGoles)); var rowsAffected = cmd.ExecuteNonQuery(); trn.Commit(); return(new OkObjectResult(historicoGoles)); } } catch (Exception ex) { trn.Rollback(); Console.WriteLine(ex.StackTrace); return(new NotFoundResult()); } } } } catch (Exception ex) { Console.WriteLine(ex.StackTrace); return(new NotFoundResult()); } }
public IActionResult Update(HistoricoGoles historicoGoles, int id) { string sql = $"UPDATE historicoGoles SET jugadorId = {historicoGoles.jugadorId}, cantGoles = {historicoGoles.cantGoles}"; try { using (SqlConnection cnn = new SqlConnection(AfaDB.cnnString)) { using (SqlCommand cmd = new SqlCommand(sql, cnn)) { cmd.ExecuteNonQuery(); return(new OkObjectResult(historicoGoles)); } } } catch (Exception ex) { Console.WriteLine(ex.StackTrace); return(new NotFoundResult()); } }