Exemplo n.º 1
0
        public string InsertOrder(Models.InsertSearch order)
        {
            string sql  = @"INSERT INTO Sales.Orders
                           (CustomerID,EmployeeID,OrderDate,RequiredDate,ShippedDate,ShipperID,Freight,ShipName,ShipAddress,ShipCity,ShipRegion,ShipPostalCode,ShipCountry) 
                           VALUES (@CustomerID,@EmployeeID,@OrderDate,@RequiredDate,@ShippedDate,@ShipperID,@Freight,@ShipName,@ShipAddress,@ShipCity,@ShipRegion,@ShipPostalCode,@ShipCountry)
                           Select SCOPE_IDENTITY()";
            string sql2 = @"INSERT INTO Sales.OrderDetails
                           (OrderID,ProductID,UnitPrice,Qty) 
                           VALUES (@OrderID,@ProductID,@UnitPrice,@Qty)";
            string OrderID;

            using (SqlConnection conn = new SqlConnection(this.GetconnectionStrings()))
            {
                conn.Open();
                SqlCommand command  = new SqlCommand(sql, conn);
                SqlCommand command2 = new SqlCommand(sql2, conn);
                command.Parameters.Add(new SqlParameter("@CustomerID", order.CustomerID));
                command.Parameters.Add(new SqlParameter("@EmployeeID", order.EmployeeID));
                command.Parameters.Add(new SqlParameter("@OrderDate", order.OrderDate));
                command.Parameters.Add(new SqlParameter("@RequiredDate", order.RequiredDate));
                command.Parameters.Add(new SqlParameter("@ShippedDate", order.ShippedDate));
                command.Parameters.Add(new SqlParameter("@ShipperID", order.ShipperID));
                command.Parameters.Add(new SqlParameter("@Freight", order.Freight));
                command.Parameters.Add(new SqlParameter("@ShipName", order.ShipName));
                command.Parameters.Add(new SqlParameter("@ShipAddress", order.ShipAddress));
                command.Parameters.Add(new SqlParameter("@ShipCity", order.ShipCity));
                command.Parameters.Add(new SqlParameter("@ShipRegion", order.ShipRegion));
                command.Parameters.Add(new SqlParameter("@ShipPostalCode", order.ShipPostalCode));
                command.Parameters.Add(new SqlParameter("@ShipCountry", order.ShipCountry));

                OrderID = command.ExecuteScalar().ToString();

                for (int i = 0; i < order.OrderDetails.Count; i++)
                {
                    command2 = new SqlCommand(sql2, conn);
                    command2.Parameters.Add(new SqlParameter("@OrderID", OrderID));
                    command2.Parameters.Add(new SqlParameter("@ProductID", order.OrderDetails[i].ProductID));
                    command2.Parameters.Add(new SqlParameter("@UnitPrice", order.OrderDetails[i].UnitPrice));
                    command2.Parameters.Add(new SqlParameter("@Qty", order.OrderDetails[i].Qty));
                    command2.ExecuteNonQuery();
                }


                conn.Close();
            }
            return(OrderID);
        }
Exemplo n.º 2
0
 public ActionResult InsertOrder(Models.InsertSearch order)
 {
     if (ModelState.IsValid)
     {
         try
         {
             CustomersService CustomersService = new CustomersService();
             CustomersService.InsertOrder(order);
             return(RedirectToAction("../Employees/Index"));
         }
         catch (Exception ex)
         {
             throw ex;
         }
     }
     return(View(order));
 }
Exemplo n.º 3
0
 public ActionResult UpdateIndex(Models.InsertSearch update)
 {
     if (ModelState.IsValid)
     {
         try
         {
             OrdersService OrdersService = new OrdersService();
             OrdersService.DeleteOrderDetailByID(update.OrderID);
             OrdersService.updateorder(update);
             return(RedirectToAction("../Employees/Index"));
         }
         catch (Exception ex)
         {
             throw ex;
         }
     }
     return(View(update));
 }