public Product GetProduct(int productId) { var product = new Product(); using (var conn = new SqlConnection(Properties.Settings.Default.NorthwindConnectionString)) { var queryToGetProductDetails = "SELECT ProductName, UnitPrice, UnitsInStock, UnitsOnOrder FROM Products WHERE ProductId = @productId"; using (var cmd = new SqlCommand(queryToGetProductDetails, conn)) { cmd.Parameters.Add(new SqlParameter("@productId", productId)); conn.Open(); using (SqlDataReader dataReader = cmd.ExecuteReader()) { while (dataReader.Read()) { product.ProductId = productId; product.ProductName = dataReader.GetString(0); product.UnitPrice = dataReader.GetDecimal(1); product.UnitsInStock = dataReader.GetInt16(2); product.UnitsOnOrder = dataReader.GetInt16(3); } } } } return product; }
public bool UpdateProduct(Product product) { var productsChanged = 0; using (var con = new SqlConnection(Properties.Settings.Default.NorthwindConnectionString)) { var productUpdateQuery = "UPDATE Products SET UnitsInStock = @unitsInStock, UnitsOnOrder = @unitsOnOrder WHERE ProductId = @productId"; using (var cmd = new SqlCommand(productUpdateQuery, con)) { cmd.Parameters.Add(new SqlParameter("@productId", product.ProductId)); cmd.Parameters.Add(new SqlParameter("@unitsInStock", product.UnitsInStock)); cmd.Parameters.Add(new SqlParameter("@unitsOnOrder", product.UnitsOnOrder)); con.Open(); productsChanged = (int)cmd.ExecuteNonQuery(); } } return (productsChanged != 0); }