Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class DAO
- {
- static void Main()
- {
- int year = 1997;
- Edit("ALFKI", "Plamen");
- //Add("NAKOVICA", "T233I");
- FindAllCustomers(year, "Canada");
- }
- static void Add(string name, string id)
- {
- Customer newCustomer = new Customer()
- {
- CompanyName = name,
- CustomerID = id
- };
- using (NORTHWNDEntities db = new NORTHWNDEntities())
- {
- bool isInDB = IsInDataBase(db, id);
- if (!isInDB)
- {
- db.Customers.Add(newCustomer);
- db.SaveChanges();
- Console.WriteLine("Added Successful.");
- }
- else
- {
- throw new ArgumentException("Such customer already exists");
- }
- }
- }
- static void Edit(string id, string newContactName)
- {
- using (NORTHWNDEntities db = new NORTHWNDEntities())
- {
- var customer = db.Customers.Where(x => x.CustomerID == id).FirstOrDefault();
- customer.ContactName = newContactName;
- db.SaveChanges();
- }
- }
- static void Delete(string id)
- {
- using (NORTHWNDEntities db = new NORTHWNDEntities())
- {
- var customer = db.Customers.Where(x => x.CustomerID == id).FirstOrDefault();
- db.Customers.Remove(customer);
- db.SaveChanges();
- }
- }
- static bool IsInDataBase(NORTHWNDEntities db, string id)
- {
- bool alreadyInDB = db.Customers.Where(a => a.CustomerID == id).Any();
- return alreadyInDB;
- }
- //Write a method that finds all customers who have orders made in 1997 and shipped to Canada.
- static void FindAllCustomers(int orderDate, string shipDestination)
- {
- using (NORTHWNDEntities db = new NORTHWNDEntities())
- {
- var orders = from order in db.Orders
- where order.OrderDate.Value.Year == orderDate && order.ShipCountry == shipDestination
- select order;
- foreach (var item in orders)
- {
- Console.WriteLine("Order made by: {0} with CustomerId: {1}", item.Customer.ContactName, item.Customer.CustomerID);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement