Beispiel #1
0
        public virtual void Excluir()
        {
            var iodb = IODB.Connect(connectionString);

            List <string> where = new List <string>();

            var propriedades = getPropriedade();

            foreach (PropertyInfo item in propriedades)
            {
                OpcoesBase opBase = (OpcoesBase)item.GetCustomAttribute(typeof(OpcoesBase));

                if (opBase != null)
                {
                    if (opBase.ChavePrimaria || opBase.UsarParaBuscar)
                    {
                        var valor = item.GetValue(this);
                        if (valor != null)
                        {
                            where.Add(item.Name + " = '" + item.GetValue(this) + "'");
                        }
                    }
                }
            }

            var queryString = "delete from " + getNomeTabela();

            if (where.Count > 0)
            {
                queryString += " where " + string.Join("and ", where);
            }

            iodb.Execute(queryString);
            iodb.Close();
        }
Beispiel #2
0
        private object BuscarUltimoId()
        {
            var    iodb          = IODB.Connect(connectionString);
            var    propriedades  = getPropriedade();
            string chavePrimaria = string.Empty;

            foreach (PropertyInfo item in propriedades)
            {
                OpcoesBase opBase = (OpcoesBase)item.GetCustomAttribute(typeof(OpcoesBase));

                if (opBase != null && opBase.ChavePrimaria)
                {
                    chavePrimaria = item.Name;
                }
            }

            string queryString = "select top(1)" + chavePrimaria + " from " + getNomeTabela() +
                                 " where " + chavePrimaria + " is not null " +
                                 "order by " + chavePrimaria + " desc";

            DataRow dr = iodb.ExecuteReturnTable(queryString).Rows[0];

            iodb.Close();

            return(dr[0]);
        }
Beispiel #3
0
        public List <IBase> Buscar()
        {
            var aux  = new List <IBase>();
            var iodb = IODB.Connect(connectionString);

            List <string> where = new List <string>();
            string chavePrimaria = string.Empty;

            var        propriedades = getPropriedade();
            OpcoesBase opBase       = null;

            foreach (PropertyInfo item in propriedades)
            {
                opBase = (OpcoesBase)item.GetCustomAttribute(typeof(OpcoesBase));

                if (opBase != null)
                {
                    if (opBase.ChavePrimaria)
                    {
                        chavePrimaria = item.Name;
                    }
                    if (opBase.UsarParaBuscar)
                    {
                        var valor = item.GetValue(this);
                        if (valor != null)
                        {
                            where.Add(item.Name + " = '" + valor + "'");
                        }
                    }
                }
            }

            var queryString = "select * from " + getNomeTabela() +
                              " where " + chavePrimaria + " is not null ";

            if (where.Count > 0)
            {
                queryString += "and " + string.Join(" and ", where);
            }
            else
            {
                throw new Exception("Para realizar a busca do(s) item(s), necessita-se de uma CHAVE referente ao objto NÃO NULA.");
            }

            SqlDataReader reader = iodb.ExecuteReturnReader(queryString);

            while (reader.Read())
            {
                var obj = (IBase)Activator.CreateInstance(this.GetType());
                setPropriedade(obj, reader);
                aux.Add(obj);
            }

            iodb.Close();

            return(aux);
        }
        /*
         * Salva o objeto no banco
         */
        public void Salvar(T obj)
        {
            var           iodb    = IODB.Connect(connectionString);
            List <string> campos  = new List <string>();
            List <string> valores = new List <string>();

            var propriedades = getPropriedades(typeof(T));

            foreach (PropertyInfo item in propriedades)
            {
                OpcoesBase opBase = (OpcoesBase)item.GetCustomAttribute(typeof(OpcoesBase));

                if (opBase != null)
                {
                    if (opBase.UsarBancoDados && !opBase.AutoIncrementa)
                    {
                        campos.Add(item.Name);
                        valores.Add("'" + item.GetValue(obj) + "'");
                    }
                }
            }

            var queryString = "insert into " + getNomeTabela(typeof(T)) + " (" + string.Join(", ", campos) + ") " +
                              "values (" + string.Join(", ", valores) + ")";

            iodb.Execute(queryString);
            iodb.Close();

            //using (SqlConnection connection = new SqlConnection(connectionString))
            //{
            //    List<string> campos = new List<string>();
            //    List<string> valores = new List<string>();

            //    var propriedades = getPropriedades(typeof(T));
            //    foreach (PropertyInfo item in propriedades)
            //    {
            //        OpcoesBase opBase = (OpcoesBase)item.GetCustomAttribute(typeof(OpcoesBase));

            //        if (opBase != null)
            //        {
            //            if (opBase.UsarBancoDados && !opBase.AutoIncrementa)
            //            {
            //                campos.Add(item.Name);
            //                valores.Add("'" + item.GetValue(obj) + "'");
            //            }
            //        }
            //    }

            //    var queryString = "insert into " + getNomeTabela(typeof(T)) + " (" + string.Join(", ", campos) + ") " +
            //                      "values (" + string.Join(", ", valores) + ")";
            //    SqlCommand command = new SqlCommand(queryString, connection);
            //    command.Connection.Open();
            //    command.ExecuteNonQuery();
            //}
        }
Beispiel #5
0
        private void setPropriedade(IBase obj, SqlDataReader reader)
        {
            var propriedades = getPropriedade();

            foreach (PropertyInfo item in propriedades)
            {
                OpcoesBase opBase = (OpcoesBase)item.GetCustomAttribute(typeof(OpcoesBase));

                if (opBase != null && opBase.UsarBancoDados && reader[item.Name] != DBNull.Value)
                {
                    item.SetValue(obj, reader[item.Name]);
                }
            }
        }
Beispiel #6
0
        //Pegar a assinatura do Nome da tabela
        private string getNomeTabela()
        {
            var        info   = getInfo();
            OpcoesBase opBase = (OpcoesBase)info.GetCustomAttribute(typeof(OpcoesBase));

            if (opBase != null)
            {
                if (!string.IsNullOrEmpty(opBase.NomeTabela))
                {
                    return(opBase.NomeTabela);
                }
            }
            return(this.GetType().Name + "s");
        }
Beispiel #7
0
        public void Salvar()
        {
            var           iodb      = IODB.Connect(connectionString);
            List <string> atributos = new List <string>();
            List <string> valores   = new List <string>();

            var propriedades = getPropriedade();

            foreach (PropertyInfo item in propriedades)
            {
                OpcoesBase opBase = (OpcoesBase)item.GetCustomAttribute(typeof(OpcoesBase));

                if (opBase != null && opBase.UsarBancoDados && !opBase.AutoIncrementa)
                {
                    if (string.IsNullOrEmpty(this.Key) || this.Key == "0")
                    {
                        atributos.Add(item.Name);
                        valores.Add(!opBase.ChavePrimaria ?
                                    "'" + verificarValor(item) + "'" :
                                    "'" + (new GeradorCodigo(BuscarUltimoId().ToString())).Gerar(opBase.Max) + "'");
                    }
                    else
                    {
                        if (opBase.ChavePrimaria || opBase.ChaveUnica)
                        {
                            atributos.Add(item.Name + " = '" + item.GetValue(this) + "'");
                        }
                        else
                        {
                            valores.Add(item.Name + " = '" + verificarValor(item) + "'");
                        }
                    }
                }
            }

            var queryString = "insert into " + getNomeTabela() + " (" + string.Join(", ", atributos) + ") " +
                              "values (" + string.Join(", ", valores) + ")";

            if (!string.IsNullOrEmpty(this.Key) && this.Key != "0")
            {
                queryString = "update " + getNomeTabela() + " set " + string.Join(", ", valores) +
                              " where " + string.Join(" and ", atributos);
            }

            iodb.Execute(queryString);
            iodb.Close();
        }
Beispiel #8
0
        public void Atualizar()
        {
            var iodb = IODB.Connect(connectionString);

            List <string> where = new List <string>();
            string nomeChavePrimaria  = string.Empty;
            string valorChavePrimaria = string.Empty;

            var propriedades = getPropriedade();

            foreach (PropertyInfo item in propriedades)
            {
                OpcoesBase opBase = (OpcoesBase)item.GetCustomAttribute(typeof(OpcoesBase));

                if (opBase != null)
                {
                    if (opBase.ChavePrimaria || opBase.ChaveUnica)
                    {
                        nomeChavePrimaria  = item.Name;
                        valorChavePrimaria = item.GetValue(this).ToString();
                    }
                }
            }

            var queryString = "select * from " + getNomeTabela();

            if (!string.IsNullOrEmpty(nomeChavePrimaria) &&
                !string.IsNullOrEmpty(valorChavePrimaria))
            {
                queryString += " where " + nomeChavePrimaria + " = '" + valorChavePrimaria + "'";
            }
            else
            {
                throw new Exception("Para realizar a busca do item, necessita-se de uma CHAVE PRIMÁRIA NÃO NULA.");
            }

            SqlDataReader reader = iodb.ExecuteReturnReader(queryString);

            while (reader.Read())
            {
                setPropriedade(this, reader);
            }

            iodb.Close();
        }
        /*
         * Busca todos os campos de uma tabela no banco
         * de acordo com as propriedades listadas como
         * UsarParaBuscar da class de assinatura OpcoesBase
         */
        public List <T> Buscar(T obj)
        {
            var objectos = new List <T>();
            var iodb     = IODB.Connect(connectionString);

            List <string> where = new List <string>();
            string chavePrimaria = string.Empty;

            var        propriedades = getPropriedades(typeof(T));
            OpcoesBase opBase       = null;

            foreach (PropertyInfo item in propriedades)
            {
                opBase = (OpcoesBase)item.GetCustomAttribute(typeof(OpcoesBase));

                if (opBase != null)
                {
                    if (opBase.ChavePrimaria)
                    {
                        chavePrimaria = item.Name;
                    }
                    if (opBase.UsarParaBuscar)
                    {
                        var valor = item.GetValue(obj);
                        if (valor != null)
                        {
                            where.Add(item.Name + " = '" + valor + "'");
                        }
                    }
                }
            }

            var queryString = "select * from " + getNomeTabela(typeof(T)) +
                              " where " + chavePrimaria + " is not null ";

            if (where.Count > 0)
            {
                queryString += "and " + string.Join(" and ", where);
            }
            else
            {
                throw new Exception("Para realizar a busca do(s) item(s), necessita-se de uma CHAVE referente ao objto NÃO NULA.");
            }

            SqlDataReader reader = iodb.ExecuteReturnReader(queryString);

            while (reader.Read())
            {
                var aux = (T)Activator.CreateInstance(obj.GetType());
                setPropriedade(aux, reader);
                objectos.Add(aux);
            }

            iodb.Close();

            return(objectos);

            //using (SqlConnection connection = new SqlConnection(connectionString))
            //{
            //    List<string> where = new List<string>();
            //    string chavePrimaria = string.Empty;

            //    var propriedades = getPropriedades(typeof(T));
            //    OpcoesBase opBase = null;
            //    foreach (PropertyInfo item in propriedades)
            //    {
            //        opBase = (OpcoesBase)item.GetCustomAttribute(typeof(OpcoesBase));

            //        if (opBase != null)
            //        {
            //            if (opBase.ChavePrimaria)
            //            {
            //                chavePrimaria = item.Name;
            //            }
            //            if (opBase.UsarParaBuscar)
            //            {
            //                var valor = item.GetValue(obj);
            //                if (valor != null)
            //                {
            //                    where.Add(item.Name + " = '" + valor + "'");
            //                }
            //            }
            //        }
            //    }

            //    var queryString = "select * from " + getNomeTabela(typeof(T)) +
            //                      " where " + chavePrimaria + " is not null ";

            //    if (where.Count > 0)
            //    {
            //        queryString += "and " + string.Join(" and ", where);
            //    }
            //    else
            //    {
            //        throw new Exception("Para realizar a busca do(s) item(s), necessita-se de uma CHAVE referente ao objto NÃO NULA.");
            //    }

            //    SqlCommand command = new SqlCommand(queryString, connection);
            //    command.Connection.Open();

            //    SqlDataReader reader = command.ExecuteReader();
            //    while (reader.Read())
            //    {
            //        var aux = (T)Activator.CreateInstance(obj.GetType());
            //        setPropriedade(aux, reader);
            //        objectos.Add(aux);
            //    }
            //}

            //return objectos;
        }
        /*
         * Exclui um registro no banco atraves do identificador do objeto
         */
        public void Excluir(T obj)
        {
            var iodb = IODB.Connect(connectionString);

            List <string> where = new List <string>();

            var propriedades = getPropriedades(typeof(T));

            foreach (PropertyInfo item in propriedades)
            {
                OpcoesBase opBase = (OpcoesBase)item.GetCustomAttribute(typeof(OpcoesBase));

                if (opBase != null)
                {
                    if (opBase.ChavePrimaria || opBase.UsarParaBuscar)
                    {
                        var valor = item.GetValue(this);
                        if (valor != null)
                        {
                            where.Add(item.Name + " = '" + item.GetValue(obj) + "'");
                        }
                    }
                }
            }

            var queryString = "delete from " + getNomeTabela(typeof(T));

            if (where.Count > 0)
            {
                queryString += " where " + string.Join("and ", where);
            }

            iodb.Execute(queryString);
            iodb.Close();
            //using (SqlConnection connection = new SqlConnection(connectionString))
            //{
            //    List<string> where = new List<string>();

            //    var propriedades = getPropriedades(typeof(T));
            //    foreach (PropertyInfo item in propriedades)
            //    {
            //        OpcoesBase opBase = (OpcoesBase)item.GetCustomAttribute(typeof(OpcoesBase));

            //        if (opBase != null)
            //        {
            //            if (opBase.ChavePrimaria || opBase.UsarParaBuscar)
            //            {
            //                var valor = item.GetValue(this);
            //                if (valor != null)
            //                    where.Add(item.Name + " = '" + item.GetValue(obj) + "'");
            //            }
            //        }
            //    }

            //    var queryString = "delete from " + getNomeTabela(typeof(T));

            //    if (where.Count > 0)
            //        queryString += " where " + string.Join("and ", where);

            //    SqlCommand command = new SqlCommand(queryString, connection);
            //    command.Connection.Open();
            //    command.ExecuteNonQuery();
            //}
        }
        /*
         * Busca os dados de um objeto pelo seu identificador
         * e atualiza esse objeto
         */
        public void Atualizar(T obj)
        {
            var iodb = IODB.Connect(connectionString);

            List <string> where = new List <string>();
            string nomeChavePrimaria  = string.Empty;
            string valorChavePrimaria = string.Empty;

            var propriedades = getPropriedades(typeof(T));

            foreach (PropertyInfo item in propriedades)
            {
                OpcoesBase opBase = (OpcoesBase)item.GetCustomAttribute(typeof(OpcoesBase));

                if (opBase != null)
                {
                    if (opBase.ChavePrimaria)
                    {
                        nomeChavePrimaria  = item.Name;
                        valorChavePrimaria = item.GetValue(this).ToString();
                    }
                }
            }

            var queryString = "select * from " + getNomeTabela(typeof(T));

            if (!string.IsNullOrEmpty(nomeChavePrimaria) &&
                !string.IsNullOrEmpty(valorChavePrimaria))
            {
                queryString += " where " + nomeChavePrimaria + " = '" + valorChavePrimaria + "'";
            }
            else
            {
                throw new Exception("Para realizar a busca do item, necessita-se de uma CHAVE PRIMÁRIA NÃO NULA.");
            }

            SqlDataReader reader = iodb.ExecuteReturnReader(queryString);

            while (reader.Read())
            {
                setPropriedade(obj, reader);
            }

            iodb.Close();
            //using (SqlConnection connection = new SqlConnection(connectionString))
            //{
            //    List<string> where = new List<string>();
            //    string nomeChavePrimaria = string.Empty;
            //    string valorChavePrimaria = string.Empty;

            //    var propriedades = getPropriedades(typeof(T));

            //    foreach (PropertyInfo item in propriedades)
            //    {
            //        OpcoesBase opBase = (OpcoesBase)item.GetCustomAttribute(typeof(OpcoesBase));

            //        if (opBase != null)
            //        {
            //            if (opBase.ChavePrimaria)
            //            {
            //                nomeChavePrimaria = item.Name;
            //                valorChavePrimaria = item.GetValue(this).ToString();
            //            }
            //        }
            //    }

            //    var queryString = "select * from " + getNomeTabela(typeof(T));

            //    if (!string.IsNullOrEmpty(nomeChavePrimaria) &&
            //        !string.IsNullOrEmpty(valorChavePrimaria))
            //    {
            //        queryString += " where " + nomeChavePrimaria + " = '" + valorChavePrimaria + "'";
            //    }
            //    else
            //    {
            //        throw new Exception("Para realizar a busca do item, necessita-se de uma CHAVE PRIMÁRIA NÃO NULA.");
            //    }

            //    SqlCommand command = new SqlCommand(queryString, connection);
            //    command.Connection.Open();

            //    SqlDataReader reader = command.ExecuteReader();
            //    while (reader.Read())
            //    {
            //        setPropriedade(obj, reader);
            //    }
            //}
        }