Example #1
0
        public static List <Item> GetAll()
        {
            DataTable   table = new DataTable();
            List <Item> Itens = new List <Item>();

            OpenConn();
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(null, dbConn);
                command.CommandText = "SELECT * FROM Item";
                NpgsqlDataAdapter Adpt = new NpgsqlDataAdapter(command);
                Adpt.Fill(table);

                if (table.Rows.Count > 0)
                {
                    foreach (DataRow dr in table.Rows)
                    {
                        Itens.Add(
                            new Item
                        {
                            ID         = Convert.ToInt32(dr["id"]),
                            Patrimonio = dr["patrimonio"].ToString(),
                            Localidade = new Localidade {
                                ID = Convert.ToInt32(dr["localidade"])
                            },
                            Categoria = new ItemCategoria {
                                ID = Convert.ToInt32(dr["categoria"])
                            },
                            Observacao = dr["observacao"].ToString(),
                            Status     = new ItemStatus {
                                Id = Convert.ToInt32(dr["status"])
                            }
                        }
                            );
                    }
                }
                foreach (Item item in Itens)
                {
                    item.Categoria = ItemCategoriaDAO.GetByID(item.Categoria.ID);
                }
                foreach (Item item in Itens)
                {
                    item.Localidade = LocalidadeDAO.GetByID(item.Localidade.ID);
                }
                foreach (Item item in Itens)
                {
                    item.Status = ItemStatusDAO.GetByID(item.Status.Id);
                }
                dbConn.Close();
                return(Itens);
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("INVENTARIO/ItemDAO/GetAll:: " + e);
                dbConn.Close();
                return(null);
            }
        }
Example #2
0
        public static Item GetByID(int Id)
        {
            NpgsqlParameter param;
            DataTable       table = new DataTable();
            Item            Item  = null;

            OpenConn();
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(null, dbConn);
                command.CommandText = "SELECT * FROM Item WHERE id = @id";

                param       = new NpgsqlParameter("@id", NpgsqlTypes.NpgsqlDbType.Integer, 0);
                param.Value = Id;
                command.Parameters.Add(param);

                NpgsqlDataAdapter Adpt = new NpgsqlDataAdapter(command);
                Adpt.Fill(table);

                if (table.Rows.Count > 0)
                {
                    foreach (DataRow dr in table.Rows)
                    {
                        Item = new Item
                        {
                            ID         = Convert.ToInt32(dr["id"]),
                            Patrimonio = dr["patrimonio"].ToString(),
                            Localidade = new Localidade {
                                ID = Convert.ToInt32(dr["localidade"])
                            },
                            Categoria = new ItemCategoria {
                                ID = Convert.ToInt32(dr["categoria"])
                            },
                            Observacao = dr["observacao"].ToString(),
                            Status     = new ItemStatus {
                                Id = Convert.ToInt32(dr["status"])
                            }
                        };
                        Item.Categoria  = ItemCategoriaDAO.GetByID(Item.Categoria.ID);
                        Item.Status     = ItemStatusDAO.GetByID(Item.Status.Id);
                        Item.Localidade = LocalidadeDAO.GetByID(Item.Localidade.ID);
                        return(Item);
                    }
                }
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("INVENTARIO/ItemDAO/GetByID:: " + e);
            }
            dbConn.Close();
            return(null);
        }