コード例 #1
0
        /// <summary>
        /// Devuelve una lista de Pianos que contiene todos los elementos de ese tipo en la BD.
        /// </summary>
        /// <returns></returns>
        public List <Piano> GetPiano()
        {
            List <Piano> pianos = new List <Piano>();
            Piano        aux    = new Piano();

            int i = 0;
            List <ETipoPiano> tiposPiano     = new List <ETipoPiano>();
            List <int>        idsInstrumento = new List <int>();

            try
            {
                this.comando = new SqlCommand();

                this.comando.CommandType = CommandType.Text;
                this.comando.CommandText = "SELECT * FROM Piano";
                this.comando.Connection  = this.conexion;

                this.conexion.Open();

                this.lector = comando.ExecuteReader();

                while (this.lector.Read())
                {
                    tiposPiano.Add((ETipoPiano)this.lector["Tipo"]);
                    idsInstrumento.Add((int)this.lector["idInstrumento"]);
                    i++;
                }

                this.conexion.Close();

                for (int j = 0; j < i; j++)
                {
                    aux = this.GenerateInstrumento(idsInstrumento[j], tiposPiano[j]);
                    pianos.Add(aux);
                }

                this.lector.Close();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (this.conexion.State == ConnectionState.Open)
                {
                    this.conexion.Close();
                }
            }

            return(pianos);
        }
コード例 #2
0
        /// <summary>
        /// Construye un Piano a partir del idInstrumento de la misma y sus propiedades.
        /// </summary>
        /// <param name="idInstrumento"></param>
        /// <param name="eTipo"></param>
        /// <returns></returns>
        private Piano GenerateInstrumento(int idInstrumento, ETipoPiano eTipoPiano)
        {
            Piano aux = new Piano();

            try
            {
                this.comando = new SqlCommand();

                this.comando.CommandType = CommandType.Text;
                this.comando.CommandText = "SELECT * FROM Instrumento WHERE id = " + idInstrumento;
                this.comando.Connection  = this.conexion;

                this.conexion.Open();

                this.lector = comando.ExecuteReader();

                while (this.lector.Read())
                {
                    aux = new Piano(this.lector["Modelo"].ToString(),
                                    (ECalidad)this.lector["Calidad"],
                                    (bool)this.lector["Usado"],
                                    this.marcas[(int)this.lector["idMarca"]],
                                    eTipoPiano,
                                    (int)this.lector["Stock"],
                                    (double)this.lector["Precio"]);
                }

                return(aux);
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
                if (this.conexion.State == ConnectionState.Open)
                {
                    this.conexion.Close();
                }
            }
        }