public void atualizarTag(CadastroTags a) { conn = classeConexão.obterConexao(); if (conn.State == ConnectionState.Open) { SqlCommand cmd = new SqlCommand("UPDATE Tags SET NomeTag=@1, DescTag=@2 where idTags=@0", conn); cmd.Parameters.AddWithValue("@1", a.getNome()); cmd.Parameters.AddWithValue("@2", a.getDesc()); 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(); } } }
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.listarTags(i) == null)) { CadastroTags a = consultaDao.listarTags(i); grdUsu.Rows.Add(); grdUsu.Rows[aa].Cells[0].Value = a.getId(); grdUsu.Rows[aa].Cells[1].Value = a.getNome(); grdUsu.Rows[aa].Cells[2].Value = a.getDesc(); aa++; } } }
public void criarTag(CadastroTags cad) { //conn = new SqlConnection(connString); conn = classeConexão.obterConexao(); if (conn.State == ConnectionState.Open) { try { // Insere na tbl Cliente string queryString = "INSERT INTO Tags (NomeTag,DescTag) VALUES (@1,@2)"; SqlCommand cmd = new SqlCommand(queryString, conn); cmd.Parameters.Add("@1", SqlDbType.NVarChar, 90).Value = cad.getNome(); cmd.Parameters.Add("@2", SqlDbType.NVarChar, 90).Value = cad.getDesc(); cmd.ExecuteScalar(); MessageBox.Show("Registro inserido com sucesso!"); } catch (Exception error) { MessageBox.Show("Erro: " + error); } finally { classeConexão.fecharConexao(); } } }
public void listarTags() { // Pega os Id da tabela do banco de dados int count = b.quantidade(); for (int i = 0; i <= count; i++) { if (b.listarTags(i) != null) { CadastroTags A = b.listarTags(i); clTags.Items.Add(A.getNome()); } } }
public void associarTag(CadastroTags tag) { if (conn.State == ConnectionState.Open && conn1.State == ConnectionState.Open && conn0.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]); } SqlCommand cmd1 = new SqlCommand("select idTags FROM Tags WHERE NomeTag=@1", conn1); cmd1.Parameters.AddWithValue("@1", tag.getNome()); SqlDataReader reader1 = cmd1.ExecuteReader(); if (reader1.Read()) { aux[1] = Convert.ToInt32(reader1[0]); } SqlCommand cmd2 = new SqlCommand("INSERT INTO DetalheTagNoticia (idNoticia,idTags) VALUES (@1,@2)", conn0); cmd2.Parameters.Add("@1", SqlDbType.NVarChar, 90).Value = aux[0]; cmd2.Parameters.Add("@2", SqlDbType.NVarChar, 90).Value = aux[1]; cmd2.ExecuteScalar(); reader.Close(); reader1.Close(); cmd.Dispose(); cmd1.Dispose(); cmd2.Dispose(); } catch (Exception error) { MessageBox.Show("Erro: " + error); } finally { classeConexão.fecharConexao(); } } }
private void btnEditar_Click(object sender, EventArgs e) { CadastroTags a = new CadastroTags(); a.setId(int.Parse(txtId.Text)); a.setNome(txtNome.Text); a.setDesc(txtDesc.Text); consultaDao.atualizarTag(a); listarTable(); txtId.Clear(); txtNome.Clear(); txtDesc.Clear(); }
public CadastroTags listarTags(int id) { conn = classeConexão.obterConexao(); if (conn.State == ConnectionState.Open) { SqlCommand cmd = new SqlCommand("select * from Tags where idTags = @id", conn); SqlDataReader reader; cmd.Parameters.AddWithValue("@id", id); reader = cmd.ExecuteReader(); try { CadastroTags a = new CadastroTags(); if (reader.Read()) { a.setId(int.Parse(reader[0].ToString())); a.setNome(reader[1].ToString()); a.setDesc(reader[2].ToString()); return(a); } else { return(null); } } catch (Exception error) { MessageBox.Show("Erro: " + error); return(null); } finally { reader.Close(); cmd.Dispose(); classeConexão.fecharConexao(); } } else { return(null); } }
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 { } } }