Nothing Special   »   [go: up one dir, main page]

Conexion 2

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient; //Agregar libreria
using System.Data;//Agregar libreria
using System.Windows.Forms;//Agregar libreria

namespace AplicacionAgenda
{
class conexion
{
SqlConnection cn = new SqlConnection("Data Source=VOSTRO;Initial
Catalog=DatosPers;Integrated Security=True"); //Cadena de conexion
//Llamar metdos del DataSet
private SqlCommandBuilder cmb;
public DataSet ds = new DataSet();
public SqlDataAdapter da;
public SqlCommand comando;

//agregar todo el metodo


public void Conectar()
{
try
{
cn.Open();
MessageBox.Show("Se conecto correctamente a la base", ".::|SIS|
Informacion");
}
catch (Exception)
{
MessageBox.Show("Error al conectar", ".::|SIS|Informacion");

}
finally
{
cn.Close();
}
}
public void consulta(string sql, string tabla)
{
ds.Tables.Clear();
da = new SqlDataAdapter(sql, cn);
cmb = new SqlCommandBuilder(da);
da.Fill(ds, tabla);
}
public bool insertar(string sql)
{
cn.Open();
comando = new SqlCommand(sql, cn);
int i = comando.ExecuteNonQuery();
cn.Close();
if (i>0)
{
return true;
}
else
{
return false;
}
}
public bool eliminar(string tabla, string condicion)
{
cn.Open();
string elimina = "Delete from " + tabla + " where " + condicion;
comando = new SqlCommand(elimina, cn);
int i = comando.ExecuteNonQuery();
cn.Close();
if(i>0)
{
return true;
}
else
{
return false;
}
}
public bool actualizar(string tabla, string campos, string condicion)
{
cn.Open();
string actualiza = "update " + tabla + " set " + campos + " where " +
condicion;
comando = new SqlCommand(actualiza, cn);
int i = comando.ExecuteNonQuery();
cn.Close();
if(i>0)
{
return true;
}
else
{
return false;
}
}

}
}
-----------------------------------------------
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;

namespace AplicacionAgenda
{
public partial class frmAgenda : Form
{
public frmAgenda()
{
InitializeComponent();
}
conexion cn = new conexion(); //creamos la instancia o el llamdo
private void frmAgenda_Load(object sender, EventArgs e)
{
cn.Conectar(); // llamos al metodo
MostrarDatos();
}
public void MostrarDatos()
{
cn.consulta("Select Codigo, Nombre, ApellidoP as 'Apellido Paterno',
ApellidoM as 'Apellido Materno', Edad, Sexo from datos order by Codigo desc",
"Datos");
dataGridView.DataSource = cn.ds.Tables["Datos"];
}

private void btnAgregar_Click(object sender, EventArgs e)


{
string Agregar = "Insert into datos values ('" + txtCodigo.Text + "','"
+ txtNombre.Text + "','" + txtApellidoP.Text + "','" + txtApellidoM.Text + "','" +
txtEdad.Text + "','" + cboSexo.Text + "')";

if (cn.insertar(Agregar))
{
MessageBox.Show("Los datos se agregarón correctamente");
txtCodigo.Clear();
txtNombre.Clear();
txtApellidoP.Clear();
txtApellidoM.Clear();
txtEdad.Clear();
cboSexo.SelectedIndex = -1;
MostrarDatos();
}
else
MessageBox.Show("No se agregaron los datos");
}

private void btnEliminar_Click(object sender, EventArgs e)


{
if (cn.eliminar("Datos", "Codigo = '" + txtCodigo.Text + "'"))
{
MessageBox.Show("Los datos fueron eliminados correctamente", ".::|
SIS| Información");
txtCodigo.Clear();
txtNombre.Clear();
txtApellidoP.Clear();
txtApellidoM.Clear();
txtEdad.Clear();
//cboSexo.SelectedIndex = -1;
cboSexo.ResetText();
MostrarDatos();
}
else
{
MessageBox.Show("error al eliminar", ".::|SIS| Información");
}
}

private void dataGridView_CellClick(object sender,


DataGridViewCellEventArgs e)
{
DataGridViewRow dgv = dataGridView.Rows[e.RowIndex];
txtCodigo.Text = dgv.Cells[0].Value.ToString();
txtNombre.Text = dgv.Cells[1].Value.ToString();
txtApellidoP.Text = dgv.Cells[2].Value.ToString();
txtApellidoM.Text = dgv.Cells[3].Value.ToString();
txtEdad.Text = dgv.Cells[4].Value.ToString();
cboSexo.Text = dgv.Cells[5].Value.ToString();

private void btnActualizar_Click(object sender, EventArgs e)


{
string actualizar = "Nombre = '" + txtNombre.Text + "', ApellidoP = '"
+ txtApellidoP.Text + "', ApellidoM = '" + txtApellidoM.Text + "', Edad = '" +
txtEdad.Text + "', Sexo = '" + cboSexo.Text + "'";

if(cn.actualizar(" Datos",actualizar, " Codigo = '" + txtCodigo.Text +


"'"))
{
MessageBox.Show("Los datos fueron Actualizados correctamente",
".::|SIS| Información");
txtCodigo.Clear();
txtNombre.Clear();
txtApellidoP.Clear();
txtApellidoM.Clear();
txtEdad.Clear();
//cboSexo.SelectedIndex = -1;
cboSexo.ResetText();
MostrarDatos();
}
else
{
MessageBox.Show("error al actualizar", ".::|SIS| Información");
}
}
}
}

You might also like