////获得订单 //public Order GetOrder(string orderId) //{ // using (var db = new OrderDB()) // { // return db.Order.Include("Orderdetails"). // SingleOrDefault(o => o.CustomerNum == orderId); // } //} //获得所有订单 public List <Order> GetAllOrders() { using (var db = new OrderDB()) { return(db.Order.Include("orderDetails").ToList <Order>()); } }
//通过客户名查询订单 public List <Order> QueryByCustomer(string cusname) { using (var db = new OrderDB()) { return(db.Order.Include("orderDetails") .Where(o => o.CustomerName.Equals(cusname)).ToList <Order>()); } }
//通过商品名查询订单 public List <Order> QueryByGoods(string comname) { using (var db = new OrderDB()) { var query = db.Order.Include("orderDetails") .Where(o => o.OrderDetails.Where( orderdetail => orderdetail.CommodityName.Equals(comname)).Count() > 0); return(query.ToList <Order>()); } }
//删除订单 public void Remove(string orderId) { using (var db = new OrderDB()) { var order = db.Order.Include("Orderdetails").SingleOrDefault(o => o.CustomerNum == orderId); db.OrderDetail.RemoveRange(order.OrderDetails); db.Order.Remove(order); db.SaveChanges(); } }
//public List<Order> Orders = new List<Order>(); //添加订单 public void Add(Order order) { using (var db = new OrderDB()) { db.Order.Add(order); //db.Order.Attach(order); //db.Entry(order).State = EntityState.Added; db.SaveChanges(); } }
//更新订单 public void Update(Order order) { using (var db = new OrderDB()) { db.Order.Attach(order); db.Entry(order).State = EntityState.Modified; order.OrderDetails.ForEach( orderdetail => db.Entry(orderdetail).State = EntityState.Modified); db.SaveChanges(); } }
//查找订单 public Order Find(OrderDB db, string s) { foreach (Order t in db.Order) { if (t.CustomerNum == s) { return(t); } } return(null); }
//删除订单 private void button4_Click(object sender, EventArgs e) { using (var db = new OrderDB()) { if (bindingSerives.Current != null) { Orders order = (Orders)bindingSerives.Current; service.DeleteOrder(order.orderNumber, order.theCustomer); } } BindingSource bs = new BindingSource(); bs.DataSource = service; bindingSerives.DataSource = bs; }