Ejemplo n.º 1
0
        static public Library.Setor FindById(long idSetor)
        {
            SqlDataReader rdr     = null;
            SqlConnection conexao = null;
            SqlCommand    comando = null;

            try
            {
                conexao = new SqlConnection(global::Connection.Connection.String());

                comando = new SqlCommand();

                string query = "SELECT * FROM Setor WHERE id='" + idSetor + "'";

                comando.CommandText = query;

                comando.Connection = conexao;

                conexao.Open();

                rdr = comando.ExecuteReader();

                while (rdr.Read())
                {
                    Library.Setor setor = new Setor();
                    setor.Id           = (long)rdr["id"];
                    setor.Nome         = rdr["nome"].ToString();
                    setor.Descricao    = rdr["descricao"].ToString();
                    setor.DataCadastro = (DateTime)rdr["dataCadastro"];

                    return(setor);
                }
            }
            catch (SqlException ex)
            {
                Library.Diagnostics.Logger.Error(ex);
            }
            catch (Exception ex)
            {
                Library.Diagnostics.Logger.Error(ex);
            }
            finally
            {
                conexao.Close();
                comando.Dispose();
            }
            return(null);
        }
Ejemplo n.º 2
0
        static public Library.Setor FindById(long idSetor)
        {
            SqlConnection  conexao = null;
            SqlDataAdapter dap     = null;
            DataSet        ds      = null;

            Library.Setor setor = null;
            try
            {
                conexao = new SqlConnection(global::Connection.Connection.String());

                dap = new SqlDataAdapter("SELECT * FROM Setor WHERE id='" + idSetor + "'", conexao);

                ds = new DataSet();

                dap.Fill(ds, "Setor");

                if (ds.Tables["Setor"].Rows.Count == 1)
                {
                    //throw new ApplicationException("DataSet está vazio!");
                    setor              = new Setor();
                    setor.Id           = (long)ds.Tables["Setor"].Rows[0]["id"];
                    setor.Nome         = ds.Tables["Setor"].Rows[0]["nome"].ToString();
                    setor.Descricao    = ds.Tables["Setor"].Rows[0]["descricao"].ToString();
                    setor.DataCadastro = (DateTime)ds.Tables["Setor"].Rows[0]["dataCadastro"];
                }
                return(setor);
            }
            catch (SqlException ex)
            {
                Library.Diagnostics.Logger.Error(ex);;
            }
            catch (Exception ex)
            {
                Library.Diagnostics.Logger.Error(ex);;
            }
            finally
            {
                conexao.Close();
                dap.Dispose();
                ds.Dispose();
            }
            return(null);
        }
Ejemplo n.º 3
0
        static public List <Library.Setor> FindAdvanced(params Library.Classes.QItem[] args)
        {
            SqlDataReader rdr     = null;
            SqlConnection conexao = null;
            SqlCommand    comando = null;

            try
            {
                conexao = new SqlConnection(global::Connection.Connection.String());

                comando = new SqlCommand();

                string query = "SELECT * FROM Setor AS s ";

                int    p   = 0;
                string pre = "";
                foreach (Library.Classes.QItem qi in args)
                {
                    if (p == 0)
                    {
                        pre = "WHERE ";
                    }
                    else
                    {
                        pre = " AND ";
                    }

                    p++;

                    switch (qi.Campo)
                    {
                    case "s.id":
                        query += pre + "(s.id = @id)";
                        comando.Parameters.AddWithValue("@id", qi.Objeto);
                        break;

                    case "s.nome":
                        query += pre + "(s.nome = @nome)";
                        comando.Parameters.AddWithValue("@nome", qi.Objeto);
                        break;

                    case "s.descricao":
                        query += pre + "(s.descricao = @descricao)";
                        comando.Parameters.AddWithValue("@descricao", qi.Objeto);
                        break;

                    case "s.dataCadastro":
                        query += pre + "(s.dataCadastro = @dataCadastro)";
                        comando.Parameters.AddWithValue("@dataCadastro", qi.Objeto);
                        break;

                    case "s.nome LIKE %%":
                        query += pre + "(s.nome LIKE '%' + @nome + '%')";
                        comando.Parameters.AddWithValue("@nome", qi.Objeto);
                        break;

                    case "ORDER BY":
                        query += " ORDER BY " + qi.Objeto;
                        break;
                    }
                }

                comando.CommandText = query;

                comando.Connection = conexao;

                conexao.Open();

                rdr = comando.ExecuteReader();

                List <Library.Setor> setores = new List <Library.Setor>();

                while (rdr.Read())
                {
                    Library.Setor setor = new Setor();
                    setor.Id           = (long)rdr["id"];
                    setor.Nome         = rdr["nome"].ToString();
                    setor.Descricao    = rdr["descricao"].ToString();
                    setor.DataCadastro = (DateTime)rdr["dataCadastro"];

                    setores.Add(setor);
                }

                return(setores);
            }
            catch (Exception ex)
            {
                Library.Diagnostics.Logger.Error(ex);
            }
            finally
            {
                conexao.Close();
                comando.Dispose();
            }
            return(null);
        }