// // get high scores from the database public List <Models.ScoreRecord> GetHighScores() { // return obj List <Models.ScoreRecord> records = new List <Models.ScoreRecord>(); // using releases held resources using (SqlConnection connection = new SqlConnection(m_connectionString)) { // get connection string // get top ten high scores ordered from highest to lowest string sql = "SELECT TOP 10 [Score],[Name] FROM[StroidsHighScore].[dbo].[ScoreView] ORDER BY[Score] DESC"; using (SqlCommand command = new SqlCommand(sql, connection)) { connection.Open(); // run command SqlDataReader reader = command.ExecuteReader(); // build up list while (reader.Read()) { Models.ScoreRecord record = new Models.ScoreRecord(); record.Name = reader["Name"].ToString(); record.Score = (int)reader["Score"]; records.Add(record); } } } return(records); }
// // Add a new high score public void AddNewScore(Models.ScoreRecord record) { // using releases held resources using (SqlConnection connection = new SqlConnection(m_connectionString)) { // get connection string // get top ten high scores ordered from highest to lowest string sql = "INSERT INTO[dbo].[Score] ([Score],[Name]) VALUES(@ScoreValue,@Name)"; using (SqlCommand command = new SqlCommand(sql, connection)) { command.Parameters.AddWithValue("ScoreValue", record.Score); command.Parameters.AddWithValue("Name", record.Name); connection.Open(); command.ExecuteNonQuery(); } } }
// POST(Insert): api/scores public void PostScore([FromBody] Models.ScoreRecord score) { m_db.AddNewScore(score); }