private static Customer FindCustomerById(string customerID) { NorthwindEntities db = new NorthwindEntities(); using (db) { Customer searchedCustomer = db.Customers.First(c=>c.CustomerID == customerID); return searchedCustomer; } }
static void Main() { NorthwindEntities db = new NorthwindEntities(); using (db) { DAO.AddCustomer("ZZZWW", "StamatCompany"); //DAO.UpdateCustomer("ZZZWW", "NewCompanyName"); //DAO.DeleteCustomer("ZZZWW"); } }
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); } }
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); } }
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(); } } }
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); } } }
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); } } }
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); } }
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); } } }
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); } } }
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; } } } }
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); } } }
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); } } }