Exemplo n.º 1
0
        /// <summary>
        /// Devuelve un ArrayList con todos los alimentos
        /// </summary>
        /// <returns>ArrayList de todos los alimentos</returns>
        public static ArrayList SelectAll()
        {
            ArrayList alimentos = new ArrayList();

            using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.ConnectionString))
            {
                conn.Open();
                string selectString = "select * from Alimento";
                using (SqlCommand selectCommand = new SqlCommand(selectString, conn))
                {
                    SqlDataReader reader = selectCommand.ExecuteReader(CommandBehavior.CloseConnection);
                    while (reader.Read())
                    {
                        Alimento a = new Alimento();
                        a.Id     = reader.GetInt32(0);
                        a.Nombre = reader.GetString(1);
                        try
                        {
                            a.Imagen  = Alimento.GetImagen(reader.GetInt32(2));
                            a.Familia = Familia.Select(reader.GetInt32(3));
                        }
                        catch (System.Data.SqlTypes.SqlNullValueException ex) {}


                        a.Descripcion = reader.GetString(4);
                        alimentos.Add(a);
                    }
                }
            }
            return(alimentos);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Devuelve un alimento dado su id
        /// </summary>
        /// <param name="Id">Id del alimento</param>
        /// <returns>El alimento</returns>
        public static Alimento Select(int Id)
        {
            Alimento a = null;

            using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.ConnectionString))
            {
                string selectString = "select * from Alimento where Id = @id";
                using (SqlCommand selectCommand = new SqlCommand(selectString, conn))
                {
                    conn.Open();
                    selectCommand.Parameters.Add("@id", SqlDbType.Int).Value = Id;
                    SqlDataReader reader = selectCommand.ExecuteReader(CommandBehavior.CloseConnection);
                    while (reader.Read())
                    {
                        a        = new Alimento();
                        a.Id     = reader.GetInt32(0);
                        a.Nombre = reader.GetString(1);
                        try
                        {
                            a.Imagen = Alimento.GetImagen(reader.GetInt32(2));
                        }catch (Exception e)
                        {
                            a.Imagen = null;
                        }
                        try
                        {
                            a.Familia = Familia.Select(reader.GetInt32(3));
                        }
                        catch (Exception e) { a.Familia = null; }

                        a.Descripcion = reader.GetString(4);
                    }
                }
            }
            return(a);
        }