Advertisement
Guest User

Untitled

a guest
Jul 12th, 2013
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.22 KB | None | 0 0
  1. class DAO
  2. {
  3.     static void Main()
  4.     {
  5.         int year = 1997;
  6.         Edit("ALFKI", "Plamen");
  7.         //Add("NAKOVICA", "T233I");
  8.         FindAllCustomers(year, "Canada");
  9.     }
  10.    
  11.     static void Add(string name, string id)
  12.     {
  13.         Customer newCustomer = new Customer()
  14.         {
  15.             CompanyName = name,
  16.             CustomerID = id
  17.         };
  18.        
  19.         using (NORTHWNDEntities db = new NORTHWNDEntities())
  20.         {
  21.             bool isInDB = IsInDataBase(db, id);
  22.  
  23.             if (!isInDB)
  24.             {
  25.                 db.Customers.Add(newCustomer);
  26.                 db.SaveChanges();
  27.                 Console.WriteLine("Added Successful.");
  28.             }
  29.             else
  30.             {
  31.                 throw new ArgumentException("Such customer already exists");
  32.             }
  33.         }
  34.     }
  35.  
  36.     static void Edit(string id, string newContactName)
  37.     {
  38.         using (NORTHWNDEntities db = new NORTHWNDEntities())
  39.         {
  40.             var customer = db.Customers.Where(x => x.CustomerID == id).FirstOrDefault();
  41.             customer.ContactName = newContactName;
  42.             db.SaveChanges();
  43.         }
  44.     }
  45.  
  46.     static void Delete(string id)
  47.     {
  48.         using (NORTHWNDEntities db = new NORTHWNDEntities())
  49.         {
  50.             var customer = db.Customers.Where(x => x.CustomerID == id).FirstOrDefault();
  51.             db.Customers.Remove(customer);
  52.             db.SaveChanges();
  53.         }
  54.     }
  55.  
  56.     static bool IsInDataBase(NORTHWNDEntities db, string id)
  57.     {
  58.         bool alreadyInDB = db.Customers.Where(a => a.CustomerID == id).Any();
  59.         return alreadyInDB;
  60.     }
  61.  
  62.     //Write a method that finds all customers who have orders made in 1997 and shipped to Canada.
  63.  
  64.     static void FindAllCustomers(int orderDate, string shipDestination)
  65.     {
  66.         using (NORTHWNDEntities db = new NORTHWNDEntities())
  67.         {
  68.             var orders = from order in db.Orders
  69.                          where order.OrderDate.Value.Year == orderDate && order.ShipCountry == shipDestination
  70.                          select order;
  71.  
  72.             foreach (var item in orders)
  73.             {
  74.                 Console.WriteLine("Order made by: {0} with CustomerId: {1}", item.Customer.ContactName, item.Customer.CustomerID);
  75.             }
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement