예제 #1
0
파일: DAO.cs 프로젝트: nzhul/TelerikAcademy
        private static Customer FindCustomerById(string customerID)
        {
            NorthwindEntities db = new NorthwindEntities();

            using (db)
            {
                Customer searchedCustomer = db.Customers.First(c=>c.CustomerID == customerID);
                return searchedCustomer;
            }
        }
예제 #2
0
 static void Main()
 {
     NorthwindEntities db = new NorthwindEntities();
     using (db)
     {
         DAO.AddCustomer("ZZZWW", "StamatCompany");
         //DAO.UpdateCustomer("ZZZWW", "NewCompanyName");
         //DAO.DeleteCustomer("ZZZWW");
     }
 }
예제 #3
0
파일: DAO.cs 프로젝트: nzhul/TelerikAcademy
 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);
     }
 }
예제 #4
0
파일: DAO.cs 프로젝트: nzhul/TelerikAcademy
 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);
     }
 }
예제 #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();
         }
     }
 }
예제 #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);
                }
            }
        }
예제 #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);
                }
            }
        }
예제 #8
0
파일: DAO.cs 프로젝트: nzhul/TelerikAcademy
 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);
     }
 }
예제 #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);
                }
            }
        }
예제 #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);
                }
            }
        }
예제 #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;
                    }
                }
            }
        }
예제 #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);
                }
            }
        }
예제 #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);
                }
            }
        }