Пример #1
0
        public void listarTable()
        {
            for (int i = 0; i < grdUsu.RowCount; i++)
            {
                grdUsu.Rows[i].DataGridView.Rows.Clear();
            }
            // Pega os Id da tabela do banco de dados
            int count = consultaDao.quantidade();
            int aa    = 0;

            for (int i = 0; i <= count; i++)
            {
                if (!(consultaDao.listarNoticia(i) == null))
                {
                    CadastroNoticia a = consultaDao.listarNoticia(i);

                    grdUsu.Rows.Add();

                    grdUsu.Rows[aa].Cells[0].Value = a.getId();
                    grdUsu.Rows[aa].Cells[1].Value = a.getTitulo();
                    grdUsu.Rows[aa].Cells[2].Value = a.getDescricao();
                    grdUsu.Rows[aa].Cells[3].Value = a.getData();

                    aa++;
                }
            }
            listarTags();
        }
Пример #2
0
        public void atualizarNoticia(CadastroNoticia a)
        {
            conn = classeConexão.obterConexao();
            if (conn.State == ConnectionState.Open)
            {
                SqlCommand cmd = new SqlCommand("UPDATE Noticia SET Titulo=@1, DescNot=@2, DataNot=@3, LinkNot=@4 where idNoticia=@0", conn);
                cmd.Parameters.AddWithValue("@1", a.getTitulo());
                cmd.Parameters.AddWithValue("@2", a.getDescricao());
                cmd.Parameters.AddWithValue("@3", DateTime.Now);
                cmd.Parameters.AddWithValue("@4", a.getLink());
                cmd.Parameters.AddWithValue("@0", a.getId());


                cmd.CommandType = CommandType.Text;
                //conn.Open();
                try
                {
                    int i = cmd.ExecuteNonQuery();

                    if (i > 0)
                    {
                        MessageBox.Show("Registro atualizado com sucesso!");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Erro: " + ex.ToString());
                }
                finally
                {
                    conn.Close();
                }
            }
        }
Пример #3
0
        public void associarTag(CadastroTags tag, CadastroNoticia not)
        {
            conn  = classeConexão.obterConexao();
            conn1 = classeConexão.obterConexao();
            if (conn.State == ConnectionState.Open && conn1.State == ConnectionState.Open)
            {
                try
                {
                    int[] aux = new int[2];

                    // Decobre o id da ultima noticia
                    SqlCommand    cmd = new SqlCommand("select Max(idNoticia) from Noticia", conn);
                    SqlDataReader reader;
                    reader = cmd.ExecuteReader();
                    if (reader.Read())
                    {
                        aux[0] = Convert.ToInt32(reader[0]);
                    }
                    conn.Dispose();

                    conn = classeConexão.obterConexao();
                    SqlCommand cmd1 = new SqlCommand("select idTags FROM Tags WHERE NomeTag=@1", conn);
                    cmd1.Parameters.AddWithValue("@1", tag.getNome());
                    SqlDataReader reader1 = cmd1.ExecuteReader();
                    if (reader1.Read())
                    {
                        aux[1] = Convert.ToInt32(reader1[0]);
                    }
                    conn.Dispose();

                    conn = classeConexão.obterConexao();
                    SqlCommand cmd0 = new SqlCommand("select * from DetalheTagNoticia where idNoticia = @1 and idTags = @2", conn1);
                    cmd0.Parameters.AddWithValue("@1", not.getId());
                    cmd0.Parameters.AddWithValue("@2", aux[1]);
                    SqlDataReader reader0;
                    reader0 = cmd0.ExecuteReader();
                    if (reader0.Read())
                    {
                        conn = classeConexão.obterConexao();
                        cmd0 = new SqlCommand("DELETE from DetalheTagNoticia where idNoticia = @1 and idTags = @2", conn);
                        cmd0.Parameters.AddWithValue("@1", not.getId());
                        cmd0.Parameters.AddWithValue("@2", aux[1]);
                        int i = cmd0.ExecuteNonQuery();
                    }
                    conn.Dispose();
                    conn1.Dispose();

                    conn = classeConexão.obterConexao();
                    SqlCommand cmd2 = new SqlCommand("INSERT INTO DetalheTagNoticia (idNoticia,idTags) VALUES (@1,@2)", conn);
                    if (not.getId() == 0)
                    {
                        cmd2.Parameters.Add("@1", SqlDbType.NVarChar, 90).Value = aux[0];
                    }
                    else
                    {
                        cmd2.Parameters.Add("@1", SqlDbType.NVarChar, 90).Value = not.getId();
                    }
                    cmd2.Parameters.Add("@2", SqlDbType.NVarChar, 90).Value = aux[1];

                    cmd2.ExecuteScalar();

                    reader.Close();
                    reader1.Close();
                    conn.Dispose();
                }
                catch (Exception error)
                {
                    MessageBox.Show("Erro: " + error);
                }
                finally
                {
                }
            }
        }