コード例 #1
0
 protected void btnAddOrder_Click(object sender, EventArgs e)
 {
     OrderService orderContext = new OrderService(new BizCustomerOrder());
     //DalCusOrders DalCus = new DalCusOrders();
     Orders order = new Orders()
     {
         CustomerID = labContactID.Text,
         EmployeeID = int.Parse(ddlEmployee.SelectedValue),
         OrderDate = DateTime.Now,
         RequiredDate = DateTime.Now.AddDays(7),
         ShippedDate = DateTime.Now.AddDays(2),
         Freight = 20,
         ShipName = ddlShippers.SelectedItem.Text,
         ORDER_DETAILS = new List<Order_Details>(new Order_Details[] {
             new Order_Details() {
                 ProductID = int.Parse(ddlProducts.SelectedValue),
                 Quantity = Int16.Parse(txtQuantity.Text),
                 UnitPrice = decimal.Parse(labUnitPrice.Text),
                 Discount = 1
             }
         })
     };
     int result = orderContext.AddOrder(order);
     if(result>0)
     {
         Alert("新增成功!");
     }
     else
     {
         Alert("新增失敗!");
     }
 }
コード例 #2
0
 public ActionResult AddOrder(AddOrderViewModel OrderViewModel)
 {
     Orders order = new Orders()
     {
         CustomerID = OrderViewModel.CustomerID,
         EmployeeID = OrderViewModel.EmployeeID,
         OrderDate = DateTime.Now,
         RequiredDate = DateTime.Now.AddDays(7),
         ShippedDate = DateTime.Now.AddDays(2),
         Freight = 20,
         ShipName = shipperContext.GetShippers().Where(c => c.ShipperID == OrderViewModel.ShipperID).FirstOrDefault().CompanyName,
         ORDER_DETAILS = new List<Order_Details>(new Order_Details[] {
             new Order_Details() {
                 ProductID = OrderViewModel.ProductID,
                 Quantity = OrderViewModel.Quantity,
                 UnitPrice = decimal.Parse(productContext.GetProductPriceByProductID(OrderViewModel.ProductID).ToString()),
                 Discount = 1
             }
         })
     };
     int result = orderContext.AddOrder(order);
     if (result > 0)
         return RedirectToAction("Index");
     else
         return View();
 }
コード例 #3
0
        /// <summary>
        /// 新增一筆訂單資料.
        /// </summary>
        /// <param name="orders"></param>
        /// <returns></returns>
        public int AddOrder(Orders orders)
        {
            string SqlOrder = @"INSERT INTO [Orders]
               ([CustomerID]
               ,[EmployeeID]
               ,[OrderDate]
               ,[RequiredDate]
               ,[ShippedDate]
               ,[Freight]
               ,[ShipName])
             VALUES
               (@CustomerID
               ,@EmployeeID
               ,@OrderDate
               ,@RequiredDate
               ,@ShippedDate
               ,@Freight
               ,@ShipName);select @@IDENTITY";

            string SqlOrderDetails = @"INSERT INTO [dbo].[Order Details]
               ([OrderID]
               ,[ProductID]
               ,[UnitPrice]
               ,[Quantity]
               ,[Discount])
             VALUES
               (@OrderID
               ,@ProductID
               ,@UnitPrice
               ,@Quantitys
               ,@Discount)";

            SqlConnection connection = new SqlConnection(new DBConn().ConnectionString);
            SqlTransaction tran = null;
            try
            {
                DataAccess dal = new DataAccess();
                int result = 0;
                connection.Open();
                tran = connection.BeginTransaction();
                SqlCommand cmd = new SqlCommand(SqlOrder, connection, tran);
                SqlParameter[] ParamOrder = new SqlParameter[] {
                    new SqlParameter("@CustomerID", orders.CustomerID),
                    new SqlParameter("@EmployeeID", orders.EmployeeID),
                    new SqlParameter("@OrderDate", orders.OrderDate),
                    new SqlParameter("@RequiredDate", orders.RequiredDate),
                    new SqlParameter("@ShippedDate", orders.ShippedDate),
                    new SqlParameter("@Freight", orders.Freight),
                    new SqlParameter("@ShipName", orders.ShipName)
                };
                object identity = dal.ExecuteScalar(cmd, SqlOrder, CommandType.Text, ref connection, ref tran, ParamOrder);
                Order_Details order_detail = orders.ORDER_DETAILS.FirstOrDefault();
                if (order_detail != null)
                {
                    SqlParameter[] ParamOrderDetails = new SqlParameter[] {
                        new SqlParameter("@OrderID", identity),
                        new SqlParameter("@ProductID", order_detail.ProductID),
                        new SqlParameter("@UnitPrice", order_detail.UnitPrice),
                        new SqlParameter("@Quantitys", order_detail.Quantity),
                        new SqlParameter("@Discount", order_detail.Discount)
                    };
                    result += dal.ExecuteSQL(cmd, SqlOrderDetails, CommandType.Text, ref connection, ref tran, ParamOrderDetails);
                }

                tran.Commit();
                return result;
            }
            catch (Exception ex)
            {
                tran.Rollback();
                throw ex;
            }
            finally
            {
                if (connection.State != ConnectionState.Closed)
                    connection.Close();
                connection.Dispose();
            }
        }
コード例 #4
0
 public int AddOrder(Orders orders)
 {
     return _Order.AddOrder(orders);
 }
コード例 #5
0
 public int AddOrder(Orders orders)
 {
     return Context.AddOrder(orders);
 }