// Create new profile using Entity Framework and save the changes to the database private void AddProfileToDatabase(String name) { if (textBoxName.Text.Length == 0) { MessageBox.Show(@"Please enter a name"); return; } if (textBoxName.Text.Length > 20) { MessageBox.Show(@"There is a limit of 20 characters" + Environment.NewLine + @"You entered " + textBoxName.Text.Length + @" characters"); return; } using (var db = new HighscoresEntities()) { var profile = new Profile {ProfileName = name}; db.Profiles.Add(profile); db.SaveChanges(); } textBoxName.Clear(); CollapseForm(); }
private void FormSelect_Load(object sender, EventArgs e) { CenterToScreen(); using (var db = new HighscoresEntities()) { var profileName = (from n in db.Profiles where n.ProfileId == _profileId select n.ProfileName).FirstOrDefault(); labelWelcome.Text = @"Welcome " + profileName + @"!"; } }
// Get the list of levels and fill the combobox with them private void GetLevels() { using (var db = new HighscoresEntities()) { var query = from l in db.Levels select l.LevelName; foreach (var level in query) { comboBoxLevels.Items.Add(level); } } }
// Get the highscores from the database for the selected level private void GetHighscores(int levelId) { using (var db = new HighscoresEntities()) { var query = from g in db.Games join p in db.Profiles on g.ProfileId equals p.ProfileId where g.LevelId == levelId orderby g.Score descending select new {p.ProfileName, g.Score, g.Date}; dataGridViewHighscores.DataSource = query.ToList(); dataGridViewHighscores.Columns[0].HeaderText = @"Name"; } }
private void FormProfile_Load(object sender, EventArgs e) { CenterToScreen(); CollapseForm(); // Create the database if it doesn't exist if (!File.Exists("Highscores.sdf")) { // Create database and add the levels to it using (var db = new HighscoresEntities()) { var level1 = new Level {LevelName = "Arithmetic"}; var level2 = new Level {LevelName = "Averages"}; var level3 = new Level {LevelName = "Exponents"}; db.Database.Create(); db.Levels.Add(level1); db.Levels.Add(level2); db.Levels.Add(level3); db.SaveChanges(); } } GetData(); }
// Get all the profile names from the database by a query and adds them all to the listbox private void GetData() { using (var db = new HighscoresEntities()) { listBoxProfile.DataSource = db.Profiles.OrderBy(n => n.ProfileId).Select(n => n.ProfileName).ToList(); } }
// Get current profile name using a query private string GetProfileName() { using (var db = new HighscoresEntities()) { return (from n in db.Profiles where n.ProfileId == _profileId select n.ProfileName).FirstOrDefault(); } }
// Enter the result into the database private void EnterResult() { using (var db = new HighscoresEntities()) { var game = new Game {Date = DateTime.Now, LevelId = _levelId, ProfileId = _profileId, Score = _score}; db.Games.Add(game); db.SaveChanges(); } }