Ejemplo n.º 1
0
        public FlowerCategory GetFlowerCategoryById(int id)
        {
            FlowerCategory category = null;

            using (DbConnection conn = new SqlConnection(ConnectionStr))
            {
                using (DbCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = @"SELECT [id]
                                                ,[name]
                                                ,[img_url]
                                        FROM [dbo].[flower_category]
                                        WHERE id = @id";
                    cmd.AddParameter("@id", System.Data.DbType.Int32, id);
                    conn.Open();
                    using (DbDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            category = new FlowerCategory()
                            {
                                Id     = reader.GetInt32(0),
                                Name   = reader.GetString(1),
                                ImgUrl = reader.GetString(2)
                            };
                        }
                    }
                }
            }
            return(category);
        }
Ejemplo n.º 2
0
        public IList <FlowerCategory> GetFlowerCategories()
        {
            IList <FlowerCategory> list = new List <FlowerCategory>();

            using (DbConnection conn = new SqlConnection(ConnectionStr))
            {
                using (DbCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = @"SELECT [id]
                                                ,[name]
                                                ,[img_url]
                                                FROM [dbo].[flower_category]";
                    // NOTE FOR COURSEWORK MARKER:
                    // IF SOME ERROR OCCURS HERE WHEN FIRST TIME RUNNING
                    // RERUN THE APPLICATION, EVERYTHING SHOULD WORK NORMALLY
                    conn.Open();
                    using (DbDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            FlowerCategory category = new FlowerCategory()
                            {
                                Id     = reader.GetInt32(0),
                                Name   = reader.GetString(1),
                                ImgUrl = reader.GetString(2)
                            };
                            list.Add(category);
                        }
                    }
                }
            }
            return(list);
        }