public IActionResult ThemMoiProduct([FromBody] InsertProductReq req) { var res = new SingleRsp(); res.Data = _svc.ThemMoiProduct(req); return(Ok(res)); }
//Đề 5: //Câu 3: Hàm thêm mới 1 record cho bảng Product public object ThemMoiProduct(InsertProductReq pro) { List <object> res = new List <object>(); var cnn = (SqlConnection)Context.Database.GetDbConnection(); if (cnn.State == ConnectionState.Closed) { cnn.Open(); } try { string sql = "insert into [dbo].[Products] ([ProductName] ,[SupplierID] ,[CategoryID] ,[QuantityPerUnit] ,[UnitPrice] ,[UnitsInStock] ,[UnitsOnOrder] ,[ReorderLevel] ,[Discontinued])"; sql = sql + "values ('" + pro.ProductName + "', '" + pro.SupplierId + "', '" + pro.CategoryId + "', '" + pro.QuantityPerUnit + "', '" + pro.UnitPrice + "', '" + pro.UnitsInStock + "', '" + pro.UnitsOnOrder + "', '" + pro.ReorderLevel + "', '" + pro.Discontinued + "')"; sql = sql + "select * from [dbo].[Products] where ProductID = @@identity"; SqlDataAdapter da = new SqlDataAdapter(); DataSet ds = new DataSet(); var cmd = cnn.CreateCommand(); cmd.CommandText = sql; cmd.CommandType = CommandType.Text; da.SelectCommand = cmd; da.Fill(ds); if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) { foreach (DataRow row in ds.Tables[0].Rows) { var x = new { ProductID = row["ProductID"], ProductName = row["ProductName"], SupplierID = row["SupplierID"], CategoryID = row["CategoryID"], QuantityPerUnit = row["QuantityPerUnit"], UnitPrice = row["UnitPrice"], UnitsInStock = row["UnitsInStock"], UnitsOnOrder = row["UnitsOnOrder"], ReorderLevel = row["ReorderLevel"], Discontinued = row["Discontinued"] }; res.Add(x); } } } catch (Exception e) { res = null; } return(res); }
public object ThemMoiProduct(InsertProductReq req) { return(_rep.ThemMoiProduct(req)); }