Skip to content

Instantly share code, notes, and snippets.

@rohansen
Created March 27, 2018 08:30
Show Gist options
  • Save rohansen/b17dd76f947b9322c2c9b620c6bed965 to your computer and use it in GitHub Desktop.
Save rohansen/b17dd76f947b9322c2c9b620c6bed965 to your computer and use it in GitHub Desktop.
public class DbGamePlayer : ICRUD<GamePlayer>
{
private string CONNECTION_STRING = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;
private SqlConnection connection;
public DbGamePlayer()
{
connection = new SqlConnection(CONNECTION_STRING);
}
public void Create(GamePlayer entity)
{
connection.Open();
using(SqlCommand cmd = connection.CreateCommand())
{
cmd.CommandText = "INSERT INTO GamePlayer (PlayerName, Highscore) VALUES (@playername, @highscore)";
cmd.Parameters.AddWithValue("playername", entity.PlayerName);
cmd.Parameters.AddWithValue("highscore", entity.Highscore);
cmd.ExecuteNonQuery();
}
connection.Close();
}
public IEnumerable<GamePlayer> GetAll()
{
List<GamePlayer> allPlayers = new List<GamePlayer>();
connection.Open();
using (SqlCommand cmd = connection.CreateCommand())
{
cmd.CommandText = "SELECT * FROM GamePlayer";
var reader = cmd.ExecuteReader();
while (reader.Read())
{
GamePlayer newGamePlayer = new GamePlayer();
newGamePlayer.Id = reader.GetInt32(reader.GetOrdinal("Id"));
newGamePlayer.PlayerName = reader.GetString(reader.GetOrdinal("PlayerName"));
newGamePlayer.Highscore = reader.GetInt32(reader.GetOrdinal("Highscore"));
allPlayers.Add(newGamePlayer);
}
}
connection.Close();
return allPlayers;
}
public void Delete(int id)
{
throw new NotImplementedException();
}
public GamePlayer Get(int id)
{
throw new NotImplementedException();
}
public void Update(GamePlayer entity)
{
throw new NotImplementedException();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment