コード例 #1
0
 // Code to execute when the application is launching (eg, from Start)
 // This code will not execute when the application is reactivated
 private void Application_Launching(object sender, LaunchingEventArgs e)
 {
     using (var db = new EscolaDataContext())
     {
         if (!db.DatabaseExists())
         {
             db.CreateDatabase();
         }
     }
 }
コード例 #2
0
        void CarregarAlunos()
        {
            using (var bd = new EscolaDataContext())
            {
                var resultado = (from aluno in bd.Alunos
                                 orderby aluno.Nome
                                 select aluno).ToList();

                listaAlunos.ItemsSource = resultado;
            }
        }
コード例 #3
0
 private void btExcluirAluno_Click(object sender, RoutedEventArgs e)
 {
     using (var bd = new EscolaDataContext())
     {
         if (MessageBox.Show("Confirma exclusão?", "Remover", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
         {
             Aluno aluno = (Aluno)((Button)sender).DataContext;
             bd.Alunos.Attach(aluno);
             bd.Alunos.DeleteOnSubmit(aluno);
             bd.SubmitChanges();
             CarregarAlunos();
         }
     }
 }
コード例 #4
0
        private void btAdicionar_Click(object sender, RoutedEventArgs e)
        {
            Aluno novoAluno = new Aluno
            {
                Nome   = txtNome.Text,
                Cidade = txtCidade.Text
            };

            using (var bd = new EscolaDataContext())
            {
                bd.Alunos.InsertOnSubmit(novoAluno);
                bd.SubmitChanges();
                CarregarAlunos();
                MessageBox.Show("Aluno adicionado!");
            }
        }