//inclui um novo aluno na tabela public void IncluirAluno(Alunos alunos) { MySqlConnection CN = new MySqlConnection(Con); MySqlCommand Com = CN.CreateCommand(); Com.CommandText = "INSERT INTO Alunos(nome,email,idade,foto) " + "Values(?nome,?email,?idade,?foto)"; Com.Parameters.AddWithValue("?nome", alunos.Nome); Com.Parameters.AddWithValue("?email", alunos.Email); Com.Parameters.AddWithValue("?idade", alunos.Idade); Com.Parameters.AddWithValue("?foto", alunos.Foto); try { CN.Open(); int registrosAfetados = Com.ExecuteNonQuery(); } catch (MySqlException ex) { throw new ApplicationException(ex.ToString()); } finally { CN.Close(); } }
//metodo que retorna uma lista de dados do SQL public List <Alunos> ListarAlunosCombo() { MySqlConnection CN = new MySqlConnection(Con); MySqlCommand cmd = CN.CreateCommand(); MySqlDataReader dr; cmd.CommandText = "select id, nome from alunos"; try { CN.Open(); cmd = new MySqlCommand(cmd.CommandText, CN); dr = cmd.ExecuteReader(); List <Alunos> alunos = new List <Alunos>(); //quando acabar as linhas que retornou da consulta, sai do While while (dr.Read()) { Alunos alu = new Alunos(); alu.AlunoID = dr.GetInt32(dr.GetOrdinal("id")); alu.Nome = dr.GetString(dr.GetOrdinal("Nome")); alunos.Add(alu); } cmd.Connection.Close(); cmd.Dispose(); return(alunos); } catch (MySqlException ex) { throw new ApplicationException(ex.ToString()); } finally { CN.Close(); } }
private void btnAlterar_Click(object sender, EventArgs e) { try { AlunosDao alunoBD = new AlunosDao(); Alunos alunoReg = new Alunos(int.Parse(txtID.Text), txtNome.Text, txtEndereco.Text, int.Parse(txtIdade.Text), txtcaminho.Text.Replace(@"\", @"\\")); //replace substitui \ por \\ para salvar alunoBD.AlterarAluno(alunoReg); MessageBox.Show("Registro alterado com sucesso."); btnExibir.PerformClick();//botãoexibir } catch (Exception c) { MessageBox.Show(c.ToString()); } }
private void btnExcluir_Click(object sender, EventArgs e) { try { AlunosDao alunoBD = new AlunosDao(); //exclui pelo ID no banco Alunos alunoReg = new Alunos(int.Parse(txtID.Text)); alunoBD.ExcluirAluno(alunoReg); MessageBox.Show("Registro excluído com sucesso."); btnAlterar.PerformClick(); } catch (Exception c) { MessageBox.Show(c.ToString()); } }
private void button1_Click(object sender, EventArgs e) {//botão salvar if (txtNome.Text.Equals("")) { MessageBox.Show("Informe o nome do aluno."); return; } try { AlunosDao alunoBD = new AlunosDao(); Alunos alunoReg = new Alunos(txtNome.Text, txtEndereco.Text, int.Parse(txtIdade.Text), txtcaminho.Text); alunoBD.IncluirAluno(alunoReg);//chamada do método MessageBox.Show("Registro salvo com sucesso."); } catch (Exception c) { MessageBox.Show(c.ToString()); } }
//exclui um aluno na tabela public void ExcluirAluno(Alunos alunos) { MySqlConnection CN = new MySqlConnection(Con); MySqlCommand Com = CN.CreateCommand(); Com.CommandText = "DELETE FROM alunos WHERE id=" + alunos.AlunoID; try { CN.Open(); int registrosAfetados = Com.ExecuteNonQuery(); } catch (MySqlException ex) { throw new ApplicationException(ex.ToString()); } finally { CN.Close(); } }
//altera um aluno na tabela public void AlterarAluno(Alunos alunos) { MySqlConnection CN = new MySqlConnection(Con); MySqlCommand Com = CN.CreateCommand(); //String foto = alunos.Foto; Com.CommandText = "UPDATE Alunos SET nome= '" + alunos.Nome + "'" + "," + " email='" + alunos.Email + "'" + "," + " idade=" + alunos.Idade + "," + " foto ='" + alunos.Foto + "' WHERE id=" + alunos.AlunoID; try { CN.Open(); int registrosAfetados = Com.ExecuteNonQuery(); } catch (MySqlException ex) { throw new ApplicationException(ex.ToString()); } finally { CN.Close(); } }