public List <ReservistaM> CarregarTodosReservista() { List <ReservistaM> listaDeRetorno = new List <ReservistaM>();//Crie uma lista de Cliente string conn = ConfigurationManager.ConnectionStrings["MySQLConnectionString"].ToString(); MySqlConnection conexao = new MySqlConnection(conn); conexao.Open(); MySqlCommand comando = new MySqlCommand(); comando = conexao.CreateCommand(); comando.CommandText = "Select * from Reservista"; MySqlDataReader reader = comando.ExecuteReader(); while (reader.Read()) //Enquanto existir dados no select { ReservistaM reservista = new ReservistaM(); //Estancia objeto do tipo cliente reservista.IdReservista = Convert.ToInt16(reader["IdReservista"]); reservista.NomeGuerra = reader["NomeGuerra"].ToString(); reservista.Profissao = reader["Profissao"].ToString(); reservista.LocalTrabalho = reader["LocalTrabalho"].ToString(); reservista.Escolaridade = reader["Escolaridade"].ToString(); //reservista.Voluntario = reader["Voluntario"]; reservista.Religiao = reader["Religiao"].ToString(); reservista.Status = reader["Folgas"].ToString(); //reservista.Patente = Convert.ToBoolean(reader["Patente"]); reservista.Ingressao = reader["Ingressao"].ToString(); listaDeRetorno.Add(reservista); //Adiciona na lista um objeto do tipo cliente } conexao.Close(); //Fecha Conexao return(listaDeRetorno); }
public int Salvar(ReservistaM reservista) { try { string conn = ConfigurationManager.ConnectionStrings["MySQLConnectionString"].ToString(); MySqlConnection conexao = new MySqlConnection(conn); conexao.Open(); MySqlCommand comando = new MySqlCommand(); comando = conexao.CreateCommand(); comando.CommandText = "insert into Reservista values(" + "@IdReservista," + "@NomeGuerra," + "@Profissao," + "@LocalTrabalho," + "@Escolaridade," + "@Voluntario," + "@Religiao," + "@Folgas," + "@Status," + "@Patente," + "@Ingressao)"; comando.Parameters.AddWithValue("@IdReservista", null); comando.Parameters.AddWithValue("@NomeGuerra", reservista.NomeGuerra); comando.Parameters.AddWithValue("@Profissao", reservista.Profissao); comando.Parameters.AddWithValue("@LocalTrabalho", reservista.LocalTrabalho); comando.Parameters.AddWithValue("@Escolaridade", reservista.Escolaridade); comando.Parameters.AddWithValue("@Voluntario", reservista.Voluntario); comando.Parameters.AddWithValue("@Religiao", reservista.Religiao); comando.Parameters.AddWithValue("@Folgas", reservista.Folgas); comando.Parameters.AddWithValue("@Status", reservista.Status); comando.Parameters.AddWithValue("@Patente", reservista.Patente); comando.Parameters.AddWithValue("@Ingressao", reservista.Ingressao); int retorno = comando.ExecuteNonQuery(); conexao.Close(); return(retorno); } catch (SqlException) { return(0); } }
private void btnSalvar_Click(object sender, EventArgs e) { reservista = new ReservistaM(); if (SimVoluntario.Checked == true) { reservista.Voluntario = true; } else { reservista.Voluntario = false; } if (PatenteSentinela.Checked == true) { reservista.Patente = true; } else { reservista.Patente = false; } reservista.NomeGuerra = txtNomeGuerra.Text.Trim(); reservista.Profissao = txtProfissao.Text.Trim(); reservista.LocalTrabalho = txtLocalTrabalho.Text.Trim(); reservista.Escolaridade = cbxEscolaridade.Text; reservista.Religiao = txtReligiao.Text.Trim(); reservista.Status = cbxSituacao.Text; reservista.Ingressao = txtIngressao.Text.Trim(); int retorno = reservistadao.Salvar(reservista); if (retorno == 1) { MessageBox.Show(" Salvo Com Sucesso! "); } else { MessageBox.Show("Deu ruim"); } }