SHOW:
|
|
- or go back to the newest paste.
1 | - | public class DAO |
1 | + | public class DAO |
2 | - | { |
2 | + | { |
3 | - | static void Main() |
3 | + | static void Main() |
4 | - | { |
4 | + | { |
5 | - | int year = 1997; |
5 | + | Edit("ALFKI", "Plamen"); |
6 | - | Edit("ALFKI", "Plamen"); |
6 | + | Add("GRIVICA", "T233I"); |
7 | - | //Add("NAKOVICA", "T233I"); |
7 | + | } |
8 | - | FindAllCustomers(year, "Canada"); |
8 | + | |
9 | - | } |
9 | + | static void Add(string name, string id) |
10 | - | |
10 | + | { |
11 | - | static void Add(string name, string id) |
11 | + | Customer newCustomer = new Customer() |
12 | - | { |
12 | + | { |
13 | - | Customer newCustomer = new Customer() |
13 | + | CompanyName = name, |
14 | - | { |
14 | + | CustomerID = id |
15 | - | CompanyName = name, |
15 | + | }; |
16 | - | CustomerID = id |
16 | + | |
17 | - | }; |
17 | + | using (NORTHWNDEntities db = new NORTHWNDEntities()) |
18 | - | |
18 | + | { |
19 | - | using (NORTHWNDEntities db = new NORTHWNDEntities()) |
19 | + | bool isInDB = IsInDataBase(db, id); |
20 | - | { |
20 | + | |
21 | - | bool isInDB = IsInDataBase(db, id); |
21 | + | if (!isInDB) |
22 | - | |
22 | + | { |
23 | - | if (!isInDB) |
23 | + | db.Customers.Add(newCustomer); |
24 | - | { |
24 | + | db.SaveChanges(); |
25 | - | db.Customers.Add(newCustomer); |
25 | + | Console.WriteLine("Added Successful."); |
26 | - | db.SaveChanges(); |
26 | + | } |
27 | - | Console.WriteLine("Added Successful."); |
27 | + | else |
28 | - | } |
28 | + | { |
29 | - | else |
29 | + | throw new ArgumentException("Such customer already exists"); |
30 | - | { |
30 | + | } |
31 | - | throw new ArgumentException("Such customer already exists"); |
31 | + | } |
32 | - | } |
32 | + | } |
33 | - | } |
33 | + | |
34 | - | } |
34 | + | static void Edit(string id, string newContactName) |
35 | - | |
35 | + | { |
36 | - | static void Edit(string id, string newContactName) |
36 | + | using (NORTHWNDEntities db = new NORTHWNDEntities()) |
37 | - | { |
37 | + | { |
38 | - | using (NORTHWNDEntities db = new NORTHWNDEntities()) |
38 | + | var customer = db.Customers.Where(x => x.CustomerID == id).FirstOrDefault(); |
39 | - | { |
39 | + | customer.ContactName = newContactName; |
40 | - | var customer = db.Customers.Where(x => x.CustomerID == id).FirstOrDefault(); |
40 | + | db.SaveChanges(); |
41 | - | customer.ContactName = newContactName; |
41 | + | } |
42 | - | db.SaveChanges(); |
42 | + | } |
43 | - | } |
43 | + | |
44 | - | } |
44 | + | static void Delete(string id) |
45 | - | |
45 | + | { |
46 | - | static void Delete(string id) |
46 | + | using (NORTHWNDEntities db = new NORTHWNDEntities()) |
47 | - | { |
47 | + | { |
48 | - | using (NORTHWNDEntities db = new NORTHWNDEntities()) |
48 | + | var customer = db.Customers.Where(x => x.CustomerID == id).FirstOrDefault(); |
49 | - | { |
49 | + | db.Customers.Remove(customer); |
50 | - | var customer = db.Customers.Where(x => x.CustomerID == id).FirstOrDefault(); |
50 | + | db.SaveChanges(); |
51 | - | db.Customers.Remove(customer); |
51 | + | } |
52 | - | db.SaveChanges(); |
52 | + | } |
53 | - | } |
53 | + | |
54 | - | } |
54 | + | static bool IsInDataBase(NORTHWNDEntities db, string id) |
55 | - | |
55 | + | { |
56 | - | static bool IsInDataBase(NORTHWNDEntities db, string id) |
56 | + | bool alreadyInDB = db.Customers.Where(a => a.CustomerID == id).Any(); |
57 | - | { |
57 | + | return alreadyInDB; |
58 | - | bool alreadyInDB = db.Customers.Where(a => a.CustomerID == id).Any(); |
58 | + | } |
59 | - | return alreadyInDB; |
59 | + |