示例#1
0
        /// <summary>
        /// get all product
        /// </summary>
        /// <returns></returns>
        public List <ProductDE> selectAll()
        {
            List <ProductDE> productList   = new List <ProductDE>();
            ProductDE        productResult = new ProductDE();

            SqlConnection connection = new SqlConnection(connectstring);

            connection.Open();
            SqlCommand command = connection.CreateCommand();

            command.CommandText = "select * from Product";

            SqlDataAdapter adapter = new SqlDataAdapter(command);
            DataTable      table   = new DataTable();

            adapter.Fill(table);
            foreach (DataRow row in table.Rows)
            {
                ProductDE product = new ProductDE()
                {
                    ProductId    = Convert.ToInt32(row["ProductId"]),
                    ProductName  = Convert.ToString(row["ProductName"]),
                    ProductPrice = Convert.ToDecimal(row["ProductPrice"]),
                    CategoryId   = Convert.ToInt32(row["CategoryId"])
                };
                CategoryDA categoryDA = new CategoryDA();
                product.Category = categoryDA.GetById(Convert.ToInt32(row["CategoryId"]));
                productList.Add(product);
            }
            connection.Close();

            return(productList);
        }
示例#2
0
        /// <summary>
        /// get product by id
        /// </summary>
        /// <param name="productid"></param>
        /// <returns></returns>
        public ProductDE GetById(int productid)
        {
            List <ProductDE> productList   = new List <ProductDE>();
            ProductDE        productResult = new ProductDE();

            if (productid >= 0)
            {
                SqlConnection connection = new SqlConnection(connectstring);
                connection.Open();
                using (SqlCommand command = connection.CreateCommand())
                {
                    command.CommandText = "select * from Product where ProductId = @ProductId";

                    command.Parameters.AddWithValue("ProductId", productid);

                    SqlDataAdapter adapter = new SqlDataAdapter(command);
                    DataTable      table   = new DataTable();
                    adapter.Fill(table);
                    foreach (DataRow row in table.Rows)
                    {
                        ProductDE product = new ProductDE()
                        {
                            ProductId    = Convert.ToInt32(row["ProductId"]),
                            ProductName  = Convert.ToString(row["ProductName"]),
                            ProductPrice = Convert.ToDecimal(row["ProductPrice"]),
                            CategoryId   = Convert.ToInt32(row["CategoryId"])
                        };
                        CategoryDA categoryDA = new CategoryDA();
                        product.Category = categoryDA.GetById(Convert.ToInt32(row["CategoryId"]));

                        productList.Add(product);
                        productResult = productList.First();
                    }
                }
            }
            return(productResult);
        }