Exemple #1
0
        private static Customer FindCustomerById(string customerID)
        {
            NorthwindEntities db = new NorthwindEntities();

            using (db)
            {
                Customer searchedCustomer = db.Customers.First(c=>c.CustomerID == customerID);
                return searchedCustomer;
            }
        }
Exemple #2
0
 static void Main()
 {
     NorthwindEntities db = new NorthwindEntities();
     using (db)
     {
         DAO.AddCustomer("ZZZWW", "StamatCompany");
         //DAO.UpdateCustomer("ZZZWW", "NewCompanyName");
         //DAO.DeleteCustomer("ZZZWW");
     }
 }
Exemple #3
0
 public static void UpdateCustomer(string customerID, string newCompanyName)
 {
     NorthwindEntities db = new NorthwindEntities();
     using (db)
     {
         Customer customerToUpdate = FindCustomerById(customerID);
         db.Entry(customerToUpdate).State = EntityState.Modified;
         customerToUpdate.CompanyName = newCompanyName;
         db.SaveChanges();
         Console.WriteLine("Customer with ID:{0} UPDATED Successfully", customerID);
     }
 }
Exemple #4
0
 public static void DeleteCustomer(string customerID)
 {
     NorthwindEntities db = new NorthwindEntities();
     using (db)
     {
         Customer customerToRemove = FindCustomerById(customerID);
         db.Entry(customerToRemove).State = EntityState.Deleted;
         db.Customers.Remove(customerToRemove);
         db.SaveChanges();
         Console.WriteLine("Customer with ID:{0} DELETED Successfully", customerID);
     }
 }
Exemple #5
0
 static void Main()
 {
     using (NorthwindEntities dbFirst = new NorthwindEntities())
     {
         using (NorthwindEntities dbSecond = new NorthwindEntities())
         {
             dbFirst.Customers.Add(new Customer() { CustomerID = "xswde", CompanyName = "CONFLICTTEST" });
             dbSecond.Customers.Add(new Customer() { CustomerID = "xldde", CompanyName = "CONFLICTTEST2" });
             dbSecond.SaveChanges();
             dbFirst.SaveChanges();
             dbSecond.SaveChanges();
         }
     }
 }
Exemple #6
0
        static void Main()
        {
            Employee extendedEmployee = new Employee();
            NorthwindEntities db = new NorthwindEntities();
            using (db)
            {
                extendedEmployee = db.Employees.Find(1);

                foreach (var item in extendedEmployee.Territories)
                {
                    Console.WriteLine("Territory description - {0}", item.TerritoryDescription);
                }
            }
        }
Exemple #7
0
        public static void Main(string[] args)
        {
            using (NorthwindEntities dbContext = new NorthwindEntities())
            {
                var result = dbContext.usp_FindIncome("Exotic Liquids", DateTime.Parse("02/02/1995"), DateTime.Parse("02/02/2000"));

                Console.WriteLine(dbContext.usp_FindIncome("Exotic Liquids", DateTime.Parse("02/02/1995"), DateTime.Parse("02/02/2000")));

                foreach (var item in result)
                {
                    Console.WriteLine("Company Name: {0}, Total Income: {1}", item.CompanyName, item.TotalIncome);
                }
            }
        }
Exemple #8
0
 public static void AddCustomer(string customerID, string companyName)
 {
     NorthwindEntities db = new NorthwindEntities();
     using (db)
     {
         Customer newCustomer = new Customer()
         {
             CustomerID = customerID,
             CompanyName = companyName
         };
         db.Customers.Add(newCustomer);
         db.SaveChanges();
         Console.WriteLine("Customer with ID:{0} and CompanyName: {1} Recorded Successfully", customerID, companyName);
     }
 }
Exemple #9
0
        public static void FindAllCustomers(int orderDate, string shipDestination)
        {
            NorthwindEntities db = new NorthwindEntities();
            using (db)
            {
                var customers =
                    from order in db.Orders
                    where order.OrderDate.Value.Year == orderDate && order.ShipCountry == shipDestination
                    select order;

                foreach (var customer in customers)
                {
                    Console.WriteLine("Customer: {0}", customer.Customer.ContactName);
                }
            }
        }
Exemple #10
0
        private static void FindAllCustomers(int orderDate, string shipDestination)
        {
            NorthwindEntities db = new NorthwindEntities();
            using (db)
            {
                string customersQuery = @"SELECT c.ContactName from Customers
                                    c INNER JOIN Orders o ON o.CustomerID = c.CustomerID 
                                    WHERE (YEAR(o.OrderDate) = {0} AND o.ShipCountry = {1});";
                object[] queryParams = { orderDate, shipDestination };

                var customersResult = db.Database.SqlQuery<string>(customersQuery, queryParams);

                foreach (var customer in customersResult)
                {
                    Console.WriteLine("Customer: {0}", customer);
                }
            }
        }
Exemple #11
0
        private static void AddOrder(string shipName, short amount, int productID, decimal unitPrice, float discount)
        {
            NorthwindEntities db = new NorthwindEntities();
            using (db)
            {
                TransactionScope scope = new TransactionScope();
                using (scope)
                {
                    try
                    {
                        Order newOrder = new Order { ShipName = shipName };
                        db.Orders.Add(newOrder);
                        db.SaveChanges();

                        var result = db.Orders.Where(x => x.ShipName == shipName);

                        using (NorthwindEntities dbContext = new NorthwindEntities())
                        {
                            foreach (var item in result)
                            {
                                Order_Detail newDetail = new Order_Detail
                                {
                                    OrderID = item.OrderID,
                                    ProductID = productID,
                                    UnitPrice = unitPrice,
                                    Discount = discount,
                                    Quantity = amount,
                                };

                                dbContext.Order_Details.Add(newDetail);
                                dbContext.SaveChanges();
                            }
                        }
                        scope.Complete();
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }
                }
            }
        }
Exemple #12
0
        static void Main()
        {
            NorthwindEntities db = new NorthwindEntities();
            using (db)
            {
                //foreach (var customer in db.Customers)
                //{
                //    Console.WriteLine(customer.CompanyName);
                //}
                 
                var customers =
                    from c in db.Customers
                    where c.City == "London"
                    select c;

                foreach (var customer in customers)
                {
                    Console.WriteLine("{0} from -> {1}", customer.ContactName ,customer.City);
                }
            }
        }
Exemple #13
0
        private static void FindSales(int startDate, int endDate, string region)
        {
            NorthwindEntities db = new NorthwindEntities();
            using (db)
            {
                var searchResult =
                    from order in db.Orders
                    join orderDetails in db.Order_Details
                    on order.OrderID equals orderDetails.OrderID
                    where order.ShipRegion == region
                    && order.OrderDate.Value.Year == startDate
                    && order.ShippedDate.Value.Year == endDate
                    select new
                    {
                        Amount = orderDetails.Quantity,
                        Region = order.ShipRegion,
                    };

                foreach (var customer in searchResult)
                {
                    Console.WriteLine("Amount: {0}, Region: {1}", customer.Amount, customer.Region);
                }
            }
        }