Vous êtes sur la page 1sur 5

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace miniProjeto
{
public partial class frmUsuario : Form
{

string caminhoConexao = "Data Source=GRU0539719W10-1;Initial


Catalog=N4;Persist Security Info=True;User ID=sa;Password=123456";

private void TestarConexao()


{

SqlConnection conn = new SqlConnection(caminhoConexao);

conn.Open();
conn.Close();

return;
}

public frmUsuario()
{
InitializeComponent();
}

private void btnSair_Click(object sender, EventArgs e)


{
Close();
}

private void frmUsuario_Load(object sender, EventArgs e)


{
TestarConexao();
}

private void btnCadastrar_Click(object sender, EventArgs e)


{

if (txtNome.Text.Trim() == "")
{
MessageBox.Show("O campo Nome deve ser preenchido");
txtNome.Text = "";
txtNome.Focus();
return;
}
if (txtLogin.Text.Trim() == "")
{
MessageBox.Show("O campo Login deve ser preenchido");
txtLogin.Text = "";
txtLogin.Focus();
return;
}

if (txtSenha.Text.Trim() == "")
{
MessageBox.Show("O campo SENHA deve ser preenchido");
txtSenha.Text = "";
txtSenha.Focus();
return;
}

if (txtCPF.MaskFull == false)
{
MessageBox.Show("O campo CPF deve ser preenchido");
txtCPF.Text = "";
txtCPF.Focus();
return;
}

if (txtStatus.SelectedIndex == -1)
{
MessageBox.Show("O campo status deve ser preenchido");
txtCPF.Text = "";
txtCPF.Focus();
return;
}

string sql = "insert into usuario values('" + txtNome.Text + "','" +


txtLogin.Text + "','" + txtSenha.Text + "','" + txtCPF.Text + "','" +
txtStatus.SelectedText + "','" + txtObs.Text + "')";

SqlConnection conn = new SqlConnection(caminhoConexao);


SqlCommand cmd = new SqlCommand(sql, conn);
cmd.CommandType = CommandType.Text;

conn.Open();

try
{
int i = cmd.ExecuteNonQuery();

if (i > 0)
{
MessageBox.Show("Cadastro realizado com sucesso!");
}
else
{
MessageBox.Show("N�o foi possivel realizar o cadastro");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());

finally
{
conn.Close();
}
}

private void txtStatus_SelectedIndexChanged(object sender, EventArgs e)


{

private void btnPesquisar_Click(object sender, EventArgs e)


{

int i;

if (!int.TryParse(txtCodigo.Text, out i))


{
MessageBox.Show("O valor do c�digo deve ser preenchido");
txtCodigo.Text = "";
txtCodigo.Focus();
return;
}

string sql = "select * from usuario where codigo_usuario = " +


txtCodigo.Text;
SqlConnection conn = new SqlConnection(caminhoConexao);
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.CommandType = CommandType.Text;
SqlDataReader leitura;
conn.Open();

try
{
leitura = cmd.ExecuteReader();

if (leitura.Read())
{
txtNome.Text = leitura[1].ToString();
txtLogin.Text = leitura[2].ToString();
txtSenha.Text = leitura[3].ToString();
txtCPF.Text = leitura[4].ToString();
txtStatus.SelectedItem = leitura[5].ToString();
txtObs.Text = leitura[6].ToString();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
throw;
}
}

private void Alterar_Click(object sender, EventArgs e)


{
string sql = "update usuario set nome_usuario='" + txtNome.Text + "',
senha_usuario='" + txtSenha.Text + "', cpf_usuario='" + txtCPF.Text + "',
obs_usuario='" + txtObs.Text + "' where codigo_usuario=" + txtCodigo.Text;

SqlConnection conn = new SqlConnection(caminhoConexao);


SqlCommand cmd = new SqlCommand(sql, conn);
cmd.CommandType = CommandType.Text;
conn.Open();

try
{
int i = cmd.ExecuteNonQuery();

if (i>0)
{
MessageBox.Show("Dados alterados com sucesso");
}

else
{
MessageBox.Show("N�o foi possivel fazer a altera��o.");
}
}
catch (Exception)
{

throw;
}
}

private void Limpar()


{

txtNome.Text = "";
txtLogin.Text = "";
txtSenha.Text = "";
txtCPF.Text = "";
txtStatus.SelectedIndex = -1;
txtObs.Text = "";
}
private void btnLimpar_Click(object sender, EventArgs e)
{

private void Excluir_Click(object sender, EventArgs e)


{
string sql = "delete from usuario where codigo_usuario=" +
txtCodigo.Text;

SqlConnection conn = new SqlConnection(caminhoConexao);


SqlCommand cmd = new SqlCommand(sql, conn);
cmd.CommandType = CommandType.Text;
conn.Open();

try
{
int i = cmd.ExecuteNonQuery();

if (i>0)
{
MessageBox.Show("Dados excluidos com sucesso");
Limpar();
txtCodigo.Text = "";
}

else
{
MessageBox.Show("N�o foi possivel fazer a altera��o.");
}
}
catch (Exception)
{

throw;
}
}
}
}

Vous aimerez peut-être aussi