Antes que todo :
public SqlConnection Con = new SqlConnection();
public SqlCommand Tab = new SqlCommand();
public SqlDataAdapter Dadap = new SqlDataAdapter();
public DataSet Dset = new DataSet();
Conectar :
protected void Conexion()
{
Con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConexionBD"].ConnectionString);
}
Desconectar :
protected void DesC()
{
Con.Close();
}
Cargar Datos a RAM :
protected void CargarTabla()
{
Con.Open();
Tab = new SqlCommand("select * from Datos_Video", Con);
Dadap.SelectCommand = Tab;
Dadap.Fill(Dset, "Datos_Video");
}
Insertar Datos
try
{
Conexion();
Con.Open();
Tab = new SqlCommand("Insert into Datos_Video values(@Rut,@Nombre,@Pelicula,@Dias,@ValD,@Total,@Desc,@TotP)", Con);
Dadap.InsertCommand = Tab;
Dadap.InsertCommand.Parameters.Add("@Rut", SqlDbType.Char);
Dadap.InsertCommand.Parameters.Add("@Nombre", SqlDbType.VarChar);
Dadap.InsertCommand.Parameters.Add("@Pelicula", SqlDbType.VarChar);
Dadap.InsertCommand.Parameters.Add("@Dias", SqlDbType.Int);
Dadap.InsertCommand.Parameters.Add("@ValD", SqlDbType.Int);
Dadap.InsertCommand.Parameters.Add("@Total", SqlDbType.Int);
Dadap.InsertCommand.Parameters.Add("@Desc", SqlDbType.Int);
Dadap.InsertCommand.Parameters.Add("@TotP", SqlDbType.Int);
Dadap.InsertCommand.Parameters["@Rut"].Value = txtRut.Text;
Dadap.InsertCommand.Parameters["@Nombre"].Value = txtNom.Text;
Dadap.InsertCommand.Parameters["@Pelicula"].Value = CboPeli.Text;
Dadap.InsertCommand.Parameters["@Dias"].Value = txtDias.Text;
Dadap.InsertCommand.Parameters["@ValD"].Value = txtValD.Text;
Dadap.InsertCommand.Parameters["@Total"].Value = lblValT.Text;
Dadap.InsertCommand.Parameters["@Desc"].Value = lblDesc.Text;
Dadap.InsertCommand.Parameters["@TotP"].Value = lblTotP.Text;
Dadap.InsertCommand.ExecuteNonQuery();
Response.Write("< h3 >Datos Guardados de manera exitosa< /h3 >");
}
catch (Exception RR)
{
Response.Write("< p >"+RR+"< /p >");
}
Eliminar Datos
Conexion();
Con.Open();
Tabb = new SqlCommand("Delete from Datos_Video Where Rut = "+txtRut.Text+"",Con);
Dadap.DeleteCommand = Tabb;
Dadap.DeleteCommand.ExcecuteNonQuery();
Response.Write("Datos Eliminados Exitosamente"):
Buscar Rut
protected void busrut(object sender, EventArgs e)
{
conectar();
DataTable tabla = new DataTable();
Tabb = new SqlCommand("select * from cliente where rut_cli='" + txtbusrut.Text.Trim() + "'", Conn);
Dadap.SelectCommand = Tabb;
Dadap.SelectCommand.ExecuteNonQuery();
Dadap.Fill(Dset2);
tabla = Dset2.Tables[0];
ListBox1.Items.Clear();
foreach (DataRow Fila in tabla.Rows)
{
this.ListBox1.Items.Add(Fila[0].ToString() + "\t" +
Fila[1].ToString() + "\t" +
Fila[2].ToString() + "\t" +
Fila[3].ToString() + "\t" +
Fila[4].ToString() + "\t" +
Fila[5].ToString() + "\t" +
Fila[6].ToString() + "\t" +
Fila[7].ToString() + "\t");
}
desconectar();
Listar
protected void btnlistarListBox_Click(object sender, EventArgs e)
{
conectar();
DataTable tabla = new DataTable();
Tabb = new SqlCommand("select * from cliente", Conn);
Dadap.SelectCommand = Tabb;
Dadap.SelectCommand.ExecuteNonQuery();
Dadap.Fill(Dset2);
tabla = Dset2.Tables[0];
ListBox1.Items.Clear();
foreach (DataRow Fila in tabla.Rows)
{
this.ListBox1.Items.Add(Fila[0].ToString() + "\t" +
Fila[1].ToString() + "\t" +
Fila[2].ToString() + "\t" +
Fila[3].ToString() + "\t" +
Fila[4].ToString() + "\t" +
Fila[5].ToString() + "\t" +
Fila[6].ToString() + "\t" +
Fila[7].ToString() + "\t");
}
desconectar();
}
Listar Tabla
protected void btnlisTabla_Click(object sender, EventArgs e)
{
conectar();
DataTable tabla = new DataTable();
Tabb = new SqlCommand("select * from cliente", Conn);
Dadap.SelectCommand = Tabb;
Dadap.SelectCommand.ExecuteNonQuery();
Dadap.Fill(Dset2);
tabla = Dset2.Tables[0];
String html = "";
foreach (DataRow Fila in tabla.Rows)
{
html += "
";
html += "" + Fila[0].ToString() + " | ";
html += "" + Fila[1].ToString() + " | ";
html += "" + Fila[2].ToString() + " | ";
html += "" + Fila[3].ToString() + " | ";
html += "" + Fila[4].ToString() + " | ";
html += "" + Fila[5].ToString() + " | ";
html += "" + Fila[6].ToString() + " | ";
html += "" + Fila[7].ToString() + " | ";
html += "
";
}
cuerpoTabla.InnerHtml = html;
desconectar();
}