Sei sulla pagina 1di 3

CODIGOS EN C#

INGRESAR LIBRO
SqlConnection con1 = new SqlConnection();
con1.ConnectionString = "Data Source = . ;initial catalog = proyecto; user id =
administrador;Integrated Security = True"; con1.Open();
SqlCommand command = con1.CreateCommand();
command.Connection = con1;
try
{
command.CommandText = "insert into libro (cod_libro, titulo, cod
_autor, cod_cat,cod_edit) values ('" + textBox1.Text.Trim() + "','" + textBox2.T
ext.Trim() + "','" + comboBox2.Text.Trim() + "','" + comboBox1.Text.Trim() + "',
'" + comboBox3.Text.Trim() + "')";
command.ExecuteNonQuery();
}
finally
{
con1.Close();
}
{
MessageBox.Show("SE HA INGRESADO EXITOSAMENTE");
textBox1.Text = "";
textBox2.Text = "";
comboBox1.Text = "";
comboBox2.Text = "";
comboBox3.Text = "";
}
try
{
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}

MODIFICAR LIBRO
SqlConnection con1 = new SqlConnection();
con1.ConnectionString = "Data Source = . ;initial catalog = proyecto
; user id = administrador;Integrated Security = True"; con1.Open();
SqlCommand command = con1.CreateCommand();
command.Connection = con1;
try
{
command.CommandText = "update libro SET titulo = '" + textBox2.T
ext.Trim() + "', cod_autor = '" + comboBox2.Text.Trim() + "', cod_cat = '" + com
boBox1.Text.Trim() + "',cod_edit = '" + comboBox3.Text.Trim() + "' WHERE (cod_li
bro = '" + textBox1.Text.Trim() + "')";
command.ExecuteNonQuery();
}
finally
{
con1.Close();
}
{
MessageBox.Show("SE HA INGRESADO EXITOSAMENTE");
textBox1.Text = "";
textBox2.Text = "";
comboBox1.Text = "";
comboBox2.Text = "";
comboBox3.Text = "";
}
try
{
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
ELIMINAR LIBRO
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source = . ;initial catalog = proyecto;
user id = administrador;Integrated Security = True";
SqlCommand comando = new SqlCommand();
comando.Connection = con;
comando.CommandText = "Delete from libro where cod_libro = '" + text
Box1.Text.Trim() + "'";
DataTable tabla = new DataTable();
SqlDataAdapter adaptador = new SqlDataAdapter();
adaptador.SelectCommand = comando;
adaptador.Fill(tabla);
if (tabla.Rows.Count > 0)
{
MessageBox.Show("VUELVA HA INGRESAR");
textBox1.Text = "";
}
else
{
MessageBox.Show("ELIMINADO");
}
try
{
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}

SELECTS
--listado de libros ordenados alfabeticamente
select titulo, cod_libro
from libro
order by titulo asc
--libro mas vendido
select top 1 Count(b.cod_libro) as numero_venta, a.titulo
from libro as a, dbo.fac_venta_libro as b, dbo.factura_venta as c
where a.cod_libro=b.cod_libro and b.num_fac=c.num_fac
group by a.titulo
order by numero_venta desc
--conocer numero de libros por categoria
select count(*), a.descripcion
from categoria as a, libro as b
where a.cod_cat=b.cod_cat
group by a.descripcion
order by a.descripcion
--autor mas vendido
select top 1 count(*) as numero, a.nomb_autor
from autor as a, libro as b, dbo.fac_venta_libro as c, factura_venta as d
where a.cod_autor=b.cod_autor and b.cod_libro=c.cod_libro and c.num_fac=d.num_fa
c
group by a.nomb_autor
order by numero desc
--libros vendidos ordenados por editorial, categoria, autor y titulo
select count(*) as numero, a.nomb_edit as editorial, b.descripcion as categoria,
c.nomb_autor as autor, d.titulo
from editorial as a, categoria as b, autor as c, libro as d, dbo.fac_venta_libro
as e, dbo.factura_venta as f
where a.cod_edit=d.cod_edit and b.cod_cat=d.cod_cat and c.cod_autor=d.cod_autor
and d.cod_libro=e.cod_libro and e.num_fac=f.num_fac group by a.nomb_edit, b.desc
ripcion, c.nomb_autor, d.titulo order by a.nomb_edit
--lista de los mejores clientes
select top 10 count(c.id_cli) as numero, a.nombre
from cliente as a, libro as b, dbo.factura_venta as c, dbo.fac_venta_libro as d
where a.ci=c.id_cli and d.num_fac=c.num_fac and b.cod_libro=d.cod_libro group by
a.nombre order by numero desc

-- lista del mejor vendedor


select top 1 count(*) as numero, a.nombre
from vendedor as a, dbo.factura_venta as b, dbo.fac_venta_libro as c where a.cod
_vend=b.cod_vend and b.num_fac=c.num_fac group by a.nombre order by numero desc

Potrebbero piacerti anche