public string OrderInsert([FromQuery] InsertOrders Insertdata, [FromBody] InsertOrderDetails IOD) { for (int i = 0; i < IOD.ProductID.Count(); i++) { //required information if (IOD.ProductID[i] == null) { return("Please input ProductId"); } if (IOD.Quantity[i] == null) { return("Please input Quantity"); } if (IOD.Discount[i] == null) { return("Please input Discount"); } } //Insert Orders var Insert = new DataInsert(); var InsertOrder = Insert.InsertOrderData(Insertdata); //Insert OrderDetails var result = Insert.InsertOrderDetailData(IOD, InsertOrder); return(result); }
public string InsertOrderDetailData(InsertOrderDetails Insertdata, int getOrderID) { var dynamicParams = new DynamicParameters();//←動態參數 SqlConnection conn = new SqlConnection("Data Source=howardorder.database.windows.net;Initial Catalog=OrderDatabase;Persist Security Info=True;User Id =howard;Password=Yihao1222"); conn.Open(); for (int i = 0; i < Insertdata.ProductID.Count(); i++) { //Insertdata.OrderID[i] = getOrderID; dynamicParams.Add("ProductID", Insertdata.ProductID[i]); var queryUnitPrice = conn.Query <InsertProduct>( @"select Unitprice From [Products] where ProductID = @ProductID" , dynamicParams); float?EndUnitPrice = queryUnitPrice.First().UnitPrice; EndUnitPrice = EndUnitPrice - (EndUnitPrice * Insertdata.Discount[i]); dynamicParams.Add("OrderID", getOrderID); dynamicParams.Add("UnitPrice", EndUnitPrice); dynamicParams.Add("Quantity", Insertdata.Quantity[i]); dynamicParams.Add("Discount", Insertdata.Discount[i]); var SQL = conn.Execute( @"insert into [Order Details] (OrderID ,ProductID ,UnitPrice ,Quantity ,Discount) Values (@OrderID ,@ProductID ,@UnitPrice ,@Quantity ,@Discount)" , dynamicParams); } conn.Close(); return("Success"); }