Advertisement
Guest User

Untitled

a guest
Jul 12th, 2013
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class DAO
  2. {
  3.     static void Main()
  4.     {
  5.         Edit("ALFKI", "Plamen");
  6.         Add("GRIVICA", "T233I");
  7.     }
  8.    
  9.     static void Add(string name, string id)
  10.     {
  11.         Customer newCustomer = new Customer()
  12.         {
  13.             CompanyName = name,
  14.             CustomerID = id
  15.         };
  16.        
  17.         using (NORTHWNDEntities db = new NORTHWNDEntities())
  18.         {
  19.             bool isInDB = IsInDataBase(db, id);
  20.  
  21.             if (!isInDB)
  22.             {
  23.                 db.Customers.Add(newCustomer);
  24.                 db.SaveChanges();
  25.                 Console.WriteLine("Added Successful.");
  26.             }
  27.             else
  28.             {
  29.                 throw new ArgumentException("Such customer already exists");
  30.             }
  31.         }
  32.     }
  33.  
  34.     static void Edit(string id, string newContactName)
  35.     {
  36.         using (NORTHWNDEntities db = new NORTHWNDEntities())
  37.         {
  38.             var customer = db.Customers.Where(x => x.CustomerID == id).FirstOrDefault();
  39.             customer.ContactName = newContactName;
  40.             db.SaveChanges();
  41.         }
  42.     }
  43.  
  44.     static void Delete(string id)
  45.     {
  46.         using (NORTHWNDEntities db = new NORTHWNDEntities())
  47.         {
  48.             var customer = db.Customers.Where(x => x.CustomerID == id).FirstOrDefault();
  49.             db.Customers.Remove(customer);
  50.             db.SaveChanges();
  51.         }
  52.     }
  53.  
  54.     static bool IsInDataBase(NORTHWNDEntities db, string id)
  55.     {
  56.         bool alreadyInDB = db.Customers.Where(a => a.CustomerID == id).Any();
  57.         return alreadyInDB;
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement