コード例 #1
0
        private void Add_Click(object sender, RoutedEventArgs e)
        {
            string name     = NameAdd.Text;
            string position = PositionAdd.Text;
            int    age;

            if (!int.TryParse(AgeAdd.Text, out age))
            {
                MessageBox.Show("Error data!");
            }
            else
            {
                if (string.IsNullOrEmpty(name) && string.IsNullOrEmpty(position))
                {
                    MessageBox.Show("Error data!");
                }
                else
                {
                    using (SoccerContext db = new SoccerContext())
                    {
                        db.Players.Add(new Player(name, age, position));
                        db.SaveChanges();
                        Players.ItemsSource = db.Players.ToArray();
                    }
                }
            }
        }
コード例 #2
0
 public MainWindow()
 {
     InitializeComponent();
     using (SoccerContext db = new SoccerContext())
     {
         db.Players.Add(new Player("Ronaldo", 36, "Forward"));
         db.SaveChanges();
         Players.ItemsSource = db.Players.ToArray();
     }
 }
コード例 #3
0
        private void Del_Click(object sender, RoutedEventArgs e)
        {
            int id;

            if (int.TryParse(IdDelete.Text, out id))
            {
                using (SoccerContext db = new SoccerContext())
                {
                    try
                    {
                        Player p = db.Players.First(k => k.Id == id);
                        db.Players.Remove(p);
                        db.SaveChanges();
                        Players.ItemsSource = db.Players.ToArray();
                    }
                    catch
                    {
                        MessageBox.Show("Data error!");
                    }
                }
            }
        }