//Metodo para atualizar a o Datagrid ou lista private void AtualizarLista() { this.MeusClientes.Clear(); string sql = @"SELECT id_cliente, nome, telefone, cpf, email, endereco from Cliente"; SqlCommand comando = new SqlCommand(sql, ConnectionFactory.CreatConnection()); SqlDataReader dados = comando.ExecuteReader(); while (dados.Read()) { CCliente c = new CCliente(); c.idcli = dados.GetInt32(0); c.nome = dados.GetString(1); c.telefone = dados.GetString(2); c.cpf = dados.GetString(3); c.email = dados.GetString(4); c.endereço = dados.GetString(5); this.MeusClientes.Add(c); } dados.Close(); dataGridViewCliente.DataSource = null; dataGridViewCliente.DataSource = this.MeusClientes; foreach (DataGridViewBand d in dataGridViewCliente.Columns) { d.ReadOnly = true; } }
//Metodo para excluir no banco de dados public int excluirCliente(CCliente c) { string sql = @"DELETE FROM cliente WHERE id_cliente=" + c.idcli; SqlCommand comando = new SqlCommand(sql, ConnectionFactory.CreatConnection()); return comando.ExecuteNonQuery(); }
//Metodo para alterar no banco de dados public int alterarCliente(CCliente c) { string sql = @"UPDATE cliente SET nome='" + c.nome + "', telefone='" + c.telefone + "',cpf='" + c.cpf + "', email='" + c.email + "', endereco='" + c.endereço + "' WHERE id_cliente=" + c.idcli; SqlCommand comando = new SqlCommand(sql, ConnectionFactory.CreatConnection()); return comando.ExecuteNonQuery(); }
public int addCliente(CCliente c) { string sql = @"INSERT INTO cliente (nome,telefone,cpf,email,endereco) VALUES ('" + c.nome + "', '" + c.telefone + "', '" + c.cpf + "', '" + c.email + "', '" + c.endereço + "')"; SqlCommand comando = new SqlCommand(sql, ConnectionFactory.CreatConnection()); return comando.ExecuteNonQuery(); }
//Botão de Cadastro private void btnAdicionarCli_Click(object sender, EventArgs e) { if (textNomeCli.Text != "") { CCliente c = new CCliente(); c.nome = textNomeCli.Text; c.telefone = maskedTextTelefonecli.Text; c.cpf = maskedTextCPFcli.Text; c.email = textemail.Text; c.endereço = textendereco.Text; BancoCliente bc1 = new BancoCliente(); bc1.addCliente(c); MessageBox.Show("Cliente cadastrado com sucesso!!!"); AtualizarLista(); LimparTela(); } }
//Botão de alterar private void btnAlterarcli_Click(object sender, EventArgs e) { if (!isCCliente()) { MessageBox.Show("Cliente não cadastrado, selecione um cliente válido."); } else { CCliente c = new CCliente(); c.idcli = id; c.nome = textNomeCli.Text; c.telefone = maskedTextTelefonecli.Text; c.cpf = maskedTextCPFcli.Text; c.email = textemail.Text; c.endereço = textendereco.Text; BancoCliente bc2 = new BancoCliente(); bc2.alterarCliente(c); MessageBox.Show("Cliente alterado com sucesso!!!"); AtualizarLista(); LimparTela(); } }
//Botão de excluir private void btnExcluircli_Click(object sender, EventArgs e) { if (!isCCliente()) { MessageBox.Show("Cliente não cadastrado, selecione um cliente válido."); } else { DialogResult resposta = MessageBox.Show("Tem certeza que deseja excluir o cliente selecionado?", "Atenção", MessageBoxButtons.YesNo); if (resposta == DialogResult.Yes) { CCliente c = new CCliente(); c.idcli = id; BancoCliente bc3 = new BancoCliente(); bc3.excluirCliente(c); MessageBox.Show("Excluido do banco"); AtualizarLista(); LimparTela(); } } }