public bool CheckIfGameExists(string gameTitle) { using (var conn = new SqlConnection(CONNSTRING)) using (var cmd = new SqlCommand("CheckIfGameExists", conn)) { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("Title", gameTitle); SqlDataReader dr = cmd.ExecuteReader(); Game game = null; if (dr.Read()) { game = new Game { Id = Convert.ToInt32(dr["Id"]), IPAddress = dr["IpAddress"].ToString(), Key = dr["Key"].ToString(), Status = dr["Status"].ToString(), Title = dr["Title"].ToString(), Votes = Convert.ToInt32(dr["Votes"]), VoteDate = Convert.ToDateTime(dr["VoteDate"]) }; } return game != null; } }
public IEnumerable<Game> GetGamesByStatus(string status) { /* * Depending on how the database is setup, this will vary. * Using some standard stuff with Stored Procedures */ List<Game> games = new List<Game>(); using (var conn = new SqlConnection(CONNSTRING)) using (var cmd = new SqlCommand("GetGamesByStatus", conn)) { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("Status", status); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { Game game = new Game { Id = Convert.ToInt32(dr["Id"]), IPAddress = dr["IpAddress"].ToString(), Key = dr["Key"].ToString(), Status = dr["Status"].ToString(), Title = dr["Title"].ToString(), Votes = Convert.ToInt32(dr["Votes"]), VoteDate = Convert.ToDateTime(dr["VoteDate"]) }; games.Add(game); } } return games; }