示例#1
0
        } // End Product GetProduct(int ID)


        public ICollection<Product> GetProducts()
        {
            SqlConnection con = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand("GetProducts", con);
            cmd.CommandType = CommandType.StoredProcedure;

            ObservableCollection<Product> products = new ObservableCollection<Product>();
            try
            {
                con.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    // Create a Product object that wraps the 
                    // current record.
                    Product product = new Product((string)reader["ModelNumber"],
                        (string)reader["ModelName"], (decimal)reader["UnitCost"],
                        (string)reader["Description"], (int)reader["CategoryID"],
                        (string)reader["CategoryName"], (string)reader["ProductImage"]);
                    // Add to collection
                    products.Add(product);
                }
            }
            finally
            {
                con.Close();
            }

            return products;
        }
示例#2
0
        public Product GetProduct(int ID)
        {
            SqlConnection con = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand("GetProductByID", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@ProductID", ID);

            try
            {
                con.Open();
                SqlDataReader reader = cmd.ExecuteReader();

                if (reader.Read())
                {
                    // Create a product object that wraps the current object
                    Product product = new Product(
                        (string)reader["ModelNumber"],
                        (string)reader["ModelName"],
                        (decimal)reader["UnitCost"],
                        (string)reader["Description"],
                        (string)reader["ProductImage"]);
                    return product;
                }
                else { return null; }
            }
            catch (Exception) { return null; }
            finally { con.Close(); }
        } // End Product GetProduct(int ID)
示例#3
0
        public bool UpdateProduct(Product product, int ID, int CatID = -1)
        {
            string sql = "";
            sql = "Update Products ";
            sql += " Set ModelNumber=@ModelNum,";
            if (CatID != -1) { sql += " CategoryID=@CatID,"; }
            sql += " ModelName=@ModelName,";
            sql += " ProductImage=@ProdImage,";
            sql += " UnitCost=@UnitCost,";
            sql += " Description=@Desc";
            sql += " Where ProductID=@ProductID";

            SqlConnection con = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand(sql, con);

            cmd.Parameters.AddWithValue("@ProductID", ID);
            if (CatID != -1) { cmd.Parameters.AddWithValue("@CatID", ID); }
            cmd.Parameters.AddWithValue("@ModelNum", product.ModelNumber);
            cmd.Parameters.AddWithValue("@ModelName", product.ModelName);
            cmd.Parameters.AddWithValue("@ProdImage", product.ProductImagePath);
            cmd.Parameters.AddWithValue("@UnitCost", product.UnitCost);
            cmd.Parameters.AddWithValue("@Desc", product.Description);

            try
            {
                con.Open();
                cmd.ExecuteNonQuery();
            }
            catch (Exception) { throw; }
            finally { con.Close(); }
            return true;
        }