Está en la página 1de 3

Código de conexión a SQL SERVER

Crear una clase donde se agregará la referencia System.Data.SqlClient; y el siguiente código:

public SqlConnection Conexion = new SqlConnection ("server=PC17\\SQLEXPRESS; database=Facturacion;


Integrated security=true");

En el Formulario:
Agregar la referencia : using System.Data.SqlClient;

Crear una variable general del tipo clase para tener acceso a la variable conexión:
Class1 cn = new Class1();

Consultar
try
{
cn.Conexion.Open();
SqlCommand cmd = new SqlCommand("Consulta_Padre", cn.Conexion);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Codigo_P", textBox1.Text);
SqlDataReader tabla;
tabla = cmd.ExecuteReader();
tabla.Read();
textBox1.Text = tabla[0].ToString();
textBox2.Text = tabla[1].ToString();
textBox3.Text = tabla[2].ToString();
textBox4.Text = tabla[3].ToString();

cn.Conexion.Close();
}
catch (Exception)
{
MessageBox.Show("No existe el registro o esta mal escrito", "Adventencia");
cn.Conexion.Close();
}

Agregar
try
{
cn.Conexion.Open();
SqlCommand cmd = new SqlCommand("Insertar_Padre", cn.Conexion);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Codigo_P", textBox1.Text);
cmd.Parameters.AddWithValue("@Nombre_P", textBox2.Text);
cmd.Parameters.AddWithValue("@Direccion", textBox3.Text);
cmd.Parameters.AddWithValue("@Telefono", textBox4.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("Los Datos se agregaron correctamente");
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
cn.Conexion.Close();
}
catch (Exception)
{
MessageBox.Show("Los Datos no fueron agregados ", "Adventencia");
cn.Conexion.Close();
}
Modificar
try
{
cn.Conexion.Open();
SqlCommand cmd = new SqlCommand("Modificar_P", cn.Conexion);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Codigo_P", textBox1.Text);
cmd.Parameters.AddWithValue("@Nombre_P", textBox2.Text);
cmd.Parameters.AddWithValue("@Direccion", textBox3.Text);
cmd.Parameters.AddWithValue("@Telefono", textBox4.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("Los Datos se modificaron correctamente");
cn.Conexion.Close();
}
catch (Exception)
{
MessageBox.Show("Los Datos no fueron modificados ", "Adventencia");
cn.Conexion.Close();
}

Eliminar
try
{
cn.Conexion.Open();
SqlCommand cmd = new SqlCommand("Eliminar_P", cn.Conexion);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Codigo_P", textBox1.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("Los Datos se Eliminaron correctamente");
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
cn.Conexion.Close();
}
catch (Exception)
{
MessageBox.Show("Los Datos no fueron Eliminados ", "Adventencia");
cn.Conexion.Close();
}
Reporte
try
{
Int32 lin;
cn.Conexion.Open();
SqlCommand cmd = new SqlCommand("Consulta_Reporte", cn.Conexion);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataReader tabla;
tabla = cmd.ExecuteReader();
lin = 0;
while (tabla.Read())
{
DG1.Rows.Add(1);
DG1[0, lin].Value = tabla["Cod_Producto"].ToString();
DG1[1, lin].Value = tabla["Descripcion"].ToString();
DG1[2, lin].Value = tabla["Precio_Compra"].ToString();
DG1[3, lin].Value = tabla["Cant_Exist"].ToString();
lin = lin + 1;
}
cn.Conexion.Close();
}
catch (Exception)
{
cn.Conexion.Close();
}

También podría gustarte