public List <Gymnast> GetGymnasts(List <Competitor> competitors) { _gymnasts = new List <Gymnast>(); if (competitors.Count == 0) { return(_gymnasts); } else { SqlConnection myConection = new SqlConnection(_connectionString); myConection.Open(); SqlCommand cmd = new SqlCommand(GetQuery(competitors), myConection); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { Gymnast gymnast = new Gymnast() { ID = Convert.ToInt32(reader["Id"]), LastName = reader["LastName"].ToString(), FirstName = reader["FirstName"].ToString(), Country = reader["Country"].ToString() }; _gymnasts.Add(gymnast); } reader.Close(); myConection.Close(); return(_gymnasts); } }
public int AddGymnast(Gymnast gymnastInfo) { using (SqlConnection myConection = new SqlConnection(_connectionString)) { SqlCommand cmd = new SqlCommand("INSERT INTO [dbo].[Gymnasts] (LastName, FirstName, Country) Output Inserted.Id VALUES (@LastName, @FirstName, @Country)", myConection); cmd.CommandType = CommandType.Text; cmd.Connection = myConection; cmd.Parameters.AddWithValue("@LastName", gymnastInfo.LastName); cmd.Parameters.AddWithValue("@FirstName", gymnastInfo.FirstName); cmd.Parameters.AddWithValue("@Country", gymnastInfo.Country); myConection.Open(); int idGymnast = (int)cmd.ExecuteScalar(); return(idGymnast); } }