Beispiel #1
0
        public static List <Receita> Ler()
        {
            List <Receita> listaReceitas = new List <Receita>();

            SqlConnection sqlConnection       = new SqlConnection(Properties.Settings.Default.connectionString);
            SqlCommand    leerReceitasCommand = new SqlCommand();

            leerReceitasCommand.Connection  = sqlConnection;
            leerReceitasCommand.CommandType = System.Data.CommandType.StoredProcedure;
            leerReceitasCommand.CommandText = "dbo.LerReceitas";

            try
            {
                sqlConnection.Open();

                SqlDataReader reader = leerReceitasCommand.ExecuteReader();

                while (reader.Read())
                {
                    Receita receita = new Receita(); //creamos objeto receta y le asignamos los valores de la linea q se lee en el momento
                    // a continuacion los valores:
                    receita.ReceitaID             = reader.GetInt32(0);
                    receita.UserID                = reader.GetInt32(1);
                    receita.Categoria.CategoriaID = reader.GetInt32(2);
                    receita.Categoria.Nome        = reader.GetString(3);
                    receita.Nome                      = reader.GetString(4);
                    receita.Descripcao                = reader.GetString(5);
                    receita.QuantidadeComensales      = reader.GetInt32(6);
                    receita.Dificuldade.DificuldadeID = reader.GetInt32(7);
                    receita.Dificuldade.Nivel         = reader.GetString(8);
                    receita.DataPublicacao            = reader.GetDateTime(9);
                    receita.Status                    = reader.GetBoolean(10);
                    receita.Duracao                   = reader.GetInt32(11);

                    /*
                     * A nuestro objeto receita todavia le falta darle un valor a la propiedad IngredientesReceita que es de la clase List<LinhaIngrediente>
                     * Quien se encarga de consultar/insertar/actualizar/etc la tabla de base de datos LinhaIngrediente is la clase LinhaIngrediente
                     * Es por eso que se tuvo que crear una funcion en dicha clase para ejecutar el store procedure dbo.LerLinhaIngredientes
                     */
                    receita.IngredientesReceita = LinhaIngrediente.LerIngredientes(receita.ReceitaID);

                    listaReceitas.Add(receita);
                }

                sqlConnection.Close();
            }
            catch (Exception)
            {
            }

            return(listaReceitas);
        }
Beispiel #2
0
        public static List <LinhaIngrediente> LerIngredientes(int receitaID)
        {
            List <LinhaIngrediente> listaIngredientes = new List <LinhaIngrediente>();

            SqlConnection sqlConnection          = new SqlConnection(Properties.Settings.Default.connectionString);
            SqlCommand    lerIngredientesCommand = new SqlCommand();

            lerIngredientesCommand.Connection  = sqlConnection;
            lerIngredientesCommand.CommandType = System.Data.CommandType.StoredProcedure;
            lerIngredientesCommand.CommandText = "dbo.LerLinhaIngredientes";
            lerIngredientesCommand.Parameters.AddWithValue("@ReceitaID", receitaID);

            try
            {
                sqlConnection.Open();

                SqlDataReader reader = lerIngredientesCommand.ExecuteReader();

                while (reader.Read())
                {
                    LinhaIngrediente ingrediente = new LinhaIngrediente();

                    ingrediente.ReceitaID = reader.GetInt32(0);
                    ingrediente.Ingrediente.IngredienteID   = reader.GetInt32(1);
                    ingrediente.Ingrediente.NomeIngrediente = reader.GetString(2);
                    ingrediente.Unidade.UnidadeID           = reader.GetInt32(3);
                    ingrediente.Unidade.NomeUnidade         = reader.GetString(4);
                    ingrediente.Quantidade = reader.GetInt32(5);

                    listaIngredientes.Add(ingrediente);
                }

                sqlConnection.Close();
            }
            catch (Exception)
            {
            }

            return(listaIngredientes);
        }