Example #1
0
        /// <summary>
        /// Varre o nome dos Parametros que estão no model através dos atributos
        /// </summary>
        /// <returns>Array de SqlParameter com os parametros para as procedures</returns>
        protected SqlParameter[] BuscaNomeParametros(ModelPai modelo, TipoComando tipoCom)
        {
            Type tipo = modelo.GetType();

            SqlParameter[] param = new SqlParameter[tipo.GetProperties().Length];
            object[]       cols;
            object         valor;

            PropertyInfo[] prop;
            try
            {
                prop = tipo.GetProperties();
                //Varre as propriedades
                //---------------------
                for (int contador = 0; contador < prop.Length; contador++)
                {
                    //Atribui os atributos de uma propriedade ao Array cols
                    //-----------------------------------------------------
                    cols = prop[contador].GetCustomAttributes(typeof(ColunasBancoDados), true);
                    if (cols.Length > 0)
                    {
                        ColunasBancoDados colunas = (ColunasBancoDados)cols[0];
                        if (tipoCom == TipoComando.insert)
                        {
                            if (colunas.ChavePrimaria == true && prop[contador].GetValue(modelo, null) == null)
                            {
                                continue;
                            }
                        }
                        valor = prop[contador].GetValue(modelo, null);
                        if (valor == null)
                        {
                            valor = DBNull.Value;
                        }
                        param[contador] = new SqlParameter("@" + colunas.NomeColuna, valor);
                    }
                }
                return(this.AjustaTamanhoArray(param));
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                param = null;
                cols  = null;
                prop  = null;
                valor = null;
            }
        }
Example #2
0
        /// <summary>
        /// Busca os parametros que são Chave Primaria
        /// </summary>
        /// <param name="tipo">Tipo do model</param>
        /// <param name="modelo">Model</param>
        /// <returns>os Parametros que são chave primaria</returns>
        private SqlParameter[] BuscaNomeParametrosChavePrimaria(ModelPai modelo)
        {
            Type tipo = modelo.GetType();
            List <SqlParameter> listaParametros = new List <SqlParameter>();

            SqlParameter[] param = null;
            object[]       cols;
            PropertyInfo[] prop;
            try
            {
                prop = tipo.GetProperties();
                //Varre as propriedades
                //---------------------
                for (int contador = 0; contador < prop.Length; contador++)
                {
                    //Atribui os atributos de uma propriedade ao Array cols
                    //-----------------------------------------------------
                    cols = prop[contador].GetCustomAttributes(typeof(ColunasBancoDados), true);
                    if (cols.Length > 0)
                    {
                        ColunasBancoDados colunas = (ColunasBancoDados)cols[0];
                        if (colunas.ChavePrimaria == true)
                        {
                            listaParametros.Add(new SqlParameter("@" + colunas.NomeColuna, prop[contador].GetValue(modelo, null)));
                        }
                    }
                }
                param = new SqlParameter[listaParametros.Count];
                for (int con = 0; con < listaParametros.Count; con++)
                {
                    param[con] = listaParametros[con];
                }
                return(param);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                param = null;
                cols  = null;
                prop  = null;
            }
        }
Example #3
0
        public void Deserialize(DataTable dtModel)
        {
            Type tipo = this.GetType();

            object[] cols;
            object   valor;

            PropertyInfo[] prop;
            try
            {
                prop = tipo.GetProperties();
                //Varre as propriedades
                //---------------------
                for (int contador = 0; contador < prop.Length; contador++)
                {
                    //Atribui os atributos de uma propriedade ao Array cols
                    //-----------------------------------------------------
                    cols = prop[contador].GetCustomAttributes(typeof(ColunasBancoDados), true);
                    if (cols.Length > 0)
                    {
                        ColunasBancoDados colunas = (ColunasBancoDados)cols[0];
                        valor = dtModel.Rows[0][colunas.NomeColuna];
                        if (valor == DBNull.Value)
                        {
                            valor = null;
                        }
                        prop[contador].SetValue(this, valor, null);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                cols = null;
                prop = null;
            }
        }