/
MainWindow.xaml.cs
109 lines (95 loc) · 3.3 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System;
using ejercicio01.MiBD;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;
namespace ejercicio01
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
if(Regex.IsMatch(nombre.Text, "^[a-zA-Z]+$") && Regex.IsMatch(sueldo.Text, @"^\d+$")){
//instanciar bd
demoEF db = new demoEF();
Empleado emp = new Empleado();
emp.Nombre = nombre.Text;
emp.Sueldo = int.Parse(sueldo.Text);
db.Empleados.Add(emp);
db.SaveChanges();
}
else { MessageBox.Show("Ingresa solo letras en @Nombre y numeros en @Sueldo "); }
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
if (Regex.IsMatch(idtext.Text, @"^\d+$") && Regex.IsMatch(nombre.Text, "^[a-zA-Z]+$") && Regex.IsMatch(sueldo.Text, @"^\d+$"))
{
demoEF db = new demoEF();
int id = int.Parse(idtext.Text);
var emp = db.Empleados.SingleOrDefault(x => x.id == id);
//where x.id == id
//select x;
if (emp != null)
{
emp.Nombre = nombre.Text;
emp.Sueldo = int.Parse(sueldo.Text);
db.SaveChanges();
}
}//if
else { MessageBox.Show("Datos invalidos, ingresa los caracteres correctos"); }
}
private void Button_Click_3(object sender, RoutedEventArgs e)
{
if(Regex.IsMatch(idtext.Text, @"^\d+$")) {
demoEF db = new demoEF();
int id = int.Parse(idtext.Text);
var emp = db.Empleados.SingleOrDefault(x => x.id == id);
//where x.id == id
//select x;
if (emp != null)
{
db.Empleados.Remove(emp);
db.SaveChanges();
}
else { MessageBox.Show("Solo numeros #id"); }
}
}
private void Button_Click_4(object sender, RoutedEventArgs e)
{
demoEF db = new demoEF();
int id = int.Parse(idtext.Text);
var registros = from s in db.Empleados
where s.id== id
select new{
s.Nombre,
s.Sueldo
};
dbgrid.ItemsSource = registros.ToList();
}
private void Button_Click_5(object sender, RoutedEventArgs e)
{
demoEF db = new demoEF();
var registros = from s in db.Empleados
select s;
dbgrid.ItemsSource = registros.ToList();
}
}
}