Exemplo n.º 1
0
 public void ModifyOrder(Order order)
 {
     DeleteOrder(order.OrderID);
     orderlist.Add(order);
     //database
     using (var db = new OrdergContext())
     {
         db.Orders.Add(order);
         db.SaveChanges();
     }
 }
Exemplo n.º 2
0
 public void DeleteOrder(int orderID)
 {
     //database
     using (var connect = new OrdergContext())
     {
         var order = connect.Orders.Include("OrderDetails").FirstOrDefault(o => o.OrderID == orderID);
         if (order != null)
         {
             connect.Orders.Remove(order);
             connect.SaveChanges();
         }
     }
 }
Exemplo n.º 3
0
        private void init()
        {
            OrderDetail od1    = new OrderDetail(1, "香蕉", 10.0, 4);
            OrderDetail od2    = new OrderDetail(2, "铅笔", 2.0, 6);
            OrderDetail od3    = new OrderDetail(3, "保温杯", 40.5, 1);
            OrderDetail od4    = new OrderDetail(4, "鲜花", 15.0, 4);
            OrderDetail od5    = new OrderDetail(5, "台灯", 30.0, 2);
            OrderDetail od6    = new OrderDetail(6, "书", 20.0, 5);
            Order       order1 = new Order(2001, "十月一日", "王小明");
            Order       order2 = new Order(2003, "十月五日", "王小明");
            Order       order3 = new Order(2022, "十一月一日", "李二百");
            Order       order4 = new Order(2025, "一月二十", "罗小");
            Order       order5 = new Order(2008, "一月五日", "孙大");

            using (var db = new OrdergContext()) {
                order1.orderDetails = new List <OrderDetail>()
                {
                    od1, od2
                };
                order1.Money = order1.getMoney();

                order2.orderDetails = new List <OrderDetail>()
                {
                    od3
                };
                order2.Money        = order2.getMoney();
                order3.orderDetails = new List <OrderDetail>()
                {
                    od4, od5
                };
                order3.Money        = order3.getMoney();
                order4.orderDetails = new List <OrderDetail>()
                {
                    od6
                };
                order4.Money = order4.getMoney();
                db.Orders.Add(order1);
                db.Orders.Add(order2);
                db.Orders.Add(order3);
                db.Orders.Add(order4);
                db.SaveChanges();
            }

            /* List<Order> orders1 = new List<Order>();
             * { orders1.Add(order1); orders1.Add(order2); orders1.Add(order3); orders1.Add(order4); orders1.Add(order5); }
             * service.orderlist = orders1;*/
        }
Exemplo n.º 4
0
 public void AddOrder(Order order)
 {
     if (orderlist.Contains(order))
     {
         throw new Exception($"订单号为{order.OrderID}的订单已存在!");
     }
     else
     {
         orderlist.Add(order);
     }
     //database
     using (var db = new OrdergContext())
     {
         db.Orders.Add(order);
         db.SaveChanges();
     }
 }
Exemplo n.º 5
0
        public void AddDetail(OrderDetail orderDetail)
        {
            if (this.orderDetails == null)
            {
                orderDetails = new List <OrderDetail>();
            }

            //orderDetails.Add(orderDetail);
            //数据库
            using (var db = new OrdergContext())
            {
                //this.orderDetails.Add(orderDetail);
                orderDetail.OrderID = this.OrderID;
                this.orderDetails.Add(orderDetail);
                //db.Entry(orderDetail).State = EntityState.Added;
                this.Money = getMoney();
                db.OrderDetails.Add(orderDetail);
                this.Money = getMoney();
                db.SaveChanges();
            }

            /*
             *  String connetStr = "server=127.0.0.1;port=3306;user=root;password=cyx123456; database=OrderDB;";
             *  MySqlConnection conn = new MySqlConnection(connetStr);
             *
             *  conn.Open();//打开通道,建立连接,可能出现异常,使用try catch语句
             *
             *  orderDetail.OrderID = this.OrderID;
             *  this.orderDetails.Add(orderDetail);
             *
             *  string sql = $"update Orders set Money={this.getMoney()} where OrderID = {this.OrderID}";
             *
             *  using (MySqlDataAdapter dataAdapter = new MySqlDataAdapter(sql, conn))
             *  {
             *
             *      using (DataTable ds = new DataTable())
             *      {
             *
             *          dataAdapter.Fill(ds);
             *          //this.dataGridView1.DataSource = ds;
             *      }
             *  }
             *  conn.Close();
             */
        }
Exemplo n.º 6
0
        public void AddDetail(OrderDetail orderDetail)
        {
            if (orderDetail == null)
            {
                return;
            }
            else
            {
                if (!orderDetails.Contains(orderDetail))
                {
                    orderDetails.Add(orderDetail);
                }
            }

            using (var db = new OrdergContext())
            {
                this.orderDetails.Add(orderDetail);
                db.OrderDetails.Add(orderDetail);
                db.SaveChanges();
            }
        }