using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace bacnodados
{
public partial class frmFunc : Form
{
private int codfunc = 0;
public frmFunc()
{
InitializeComponent();
}
private void frmFunc_Load(object sender, EventArgs e)
{
string stringConexao = "Data Source=VAIO\\SQLEXPRESS;Initial
Catalog=bd;Integrated Security=True";
SqlConnection cn = new SqlConnection();
cn.ConnectionString = stringConexao;
try
{
cn.Open();
SqlCommand cd = new SqlCommand();
cd.CommandText = "select cargo from cargos order by cargo";
cd.Connection = cn;
SqlDataReader dr = cd.ExecuteReader();
while (dr.Read())
{
cboCargos.Items.Add(dr["cargo"]);
}
}
catch (SqlException x)
{
MessageBox.Show(x.Message, "Atenção");
}
cn.Close();
}
private void limpaTela()
{
txtNome.Clear();
cboCargos.SelectedIndex = -1;
txtSalario.Clear();
txtEmail.Clear();
txtNome.Focus();
}
private void btnSair_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnLimpar_Click(object sender, EventArgs e)
{
limpaTela();
}
private void btnIncluir_Click(object sender, EventArgs e)
{
string stringConexao = "Data Source=VAIO\\SQLEXPRESS;Initial
Catalog=bd;Integrated Security=True";
SqlConnection cn = new SqlConnection();
cn.ConnectionString = stringConexao;
try
{
cn.Open();
SqlCommand cd = new SqlCommand();
string sql = "Insert into Func (Nome,Cargo,Salario,Email) Values
('" + txtNome.Text + "','" + cboCargos.SelectedItem + "'," + txtSalario.Text + ",'"
+ txtEmail.Text + "')";
cd.CommandText = sql;
cd.Connection = cn;
cd.ExecuteNonQuery();
cn.Close();
limpaTela();
}
catch (SqlException x)
{
MessageBox.Show(x.Message, "Atenção");
}
private void btnAlterar_Click(object sender, EventArgs e)
{
string stringConexao = "Data Source=VAIO\\SQLEXPRESS;Initial
Catalog=bd;Integrated Security=True";
SqlConnection cn = new SqlConnection();
cn.ConnectionString = stringConexao;
try
{
cn.Open();
SqlCommand cd = new SqlCommand();
string sql = "Update Func set Nome='" + txtNome.Text + "'";
sql += " , Cargo='" + cboCargos.SelectedItem + "'";
sql += " , Salario=" + txtSalario.Text;
sql += " , Email='" + txtEmail.Text + "'";
sql += " where codfunc=" + codfunc;
cd.CommandText = sql;
cd.Connection = cn;
cd.ExecuteNonQuery();
cn.Close();
limpaTela();
}
catch (SqlException x)
{
MessageBox.Show(x.Message, "Atenção");
}
private void btnExcluir_Click(object sender, EventArgs e)
{
string stringConexao = "Data Source=VAIO\\SQLEXPRESS;Initial
Catalog=bd;Integrated Security=True";
SqlConnection cn = new SqlConnection();
cn.ConnectionString = stringConexao;
try
{
cn.Open();
SqlCommand cd = new SqlCommand();
string mensagem = "Confirma a Exclusão";
MessageBoxButtons botao = MessageBoxButtons.YesNo;
DialogResult resultado = MessageBox.Show(mensagem, "Alerta", botao,
MessageBoxIcon.Question);
if (resultado == DialogResult.Yes)
{
string sql = "Delete from func";
sql += " where codfunc=" + codfunc;
cd.CommandText = sql;
cd.Connection = cn;
cd.ExecuteNonQuery();
cn.Close();
}
}
catch (SqlException x)
{
MessageBox.Show(x.Message, "Atenção");
}
limpaTela();
private void btnConsultar_Click(object sender, EventArgs e)
{
frmListaFunc cl = new frmListaFunc();
cl.Show();
}
}
}