Clave primaria
de
Debemos agregar al archivo el elemento connectionStrings:
datos.
Altas
Activemos desde el Visual Studio la pestaa alta.aspx para elaborar la
interfaz visual que nos permita efectuar la carga de datos de usuarios:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Data.SqlClient;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
publicpartialclassalta:
System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,
EventArgse)
{
}
protectedvoidButton1_Click(object
sender,EventArgse)
{
strings=
System.Configuration.ConfigurationManager.Con
nectionStrings["cadenaconexion1"].ConnectionS
tring;
SqlConnectionconexion=new
SqlConnection(s);
conexion.Open();
SqlCommandcomando=new
SqlCommand("insertinto
usuarios(nombre,clave,mail)values('"+
TextBox1.Text+"','"+
this.TextBox2.Text+"','"+
TextBox3.Text+"')",
conexion);
comando.ExecuteNonQuery();
Label1.Text="Seregistroel
usuario";
conexion.Close();
}
}
Lo primero que debemos hacer es importar el espacio de nombres donde
se encuentra definida la clase SqlException:
using System.Data.SqlClient;
Abrimos la conexin:
conexion.Open();
comando.ExecuteNonQuery();
Cerramos la conexin:
conexion.Close();
Consultas
Seleccionamos del Explorador de soluciones la pgina consulta.aspx y
procedemos a elaborar la siguiente interfaz visual (disponemos un
TextBox, un Button, una Label y un HyperLink:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Data.SqlClient;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
publicpartialclassconsulta:
System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,
EventArgse)
{
}
protectedvoidButton1_Click(object
sender,EventArgse)
{
strings=
System.Configuration.ConfigurationManager.Con
nectionStrings["cadenaconexion1"].ConnectionS
tring;
SqlConnectionconexion=new
SqlConnection(s);
conexion.Open();
SqlCommandcomando=new
SqlCommand("selectnombre,clave,mailfrom
usuarios"+
"wherenombre='"+
this.TextBox1.Text+"'",conexion);
SqlDataReaderregistro=
comando.ExecuteReader();
if(registro.Read())
this.Label1.Text="Clave:"+
registro["clave"]+"<br>"+
"Mail:"+
registro["mail"];
else
this.Label1.Text="Noexisteun
usuariocondichonombre";
conexion.Close();
}
}
Para poder recuperar los datos lo hacemos creando un objeto de la clase
SqlDataReader e inicializndolo mediante la llamada del mtodo
ExecuteReader de la clase SQLCommand:
SqlDataReader registro = comando.ExecuteReader();
Baja
Seleccionamos del Explorador de soluciones la pgina baja.aspx y
procedemos a elaborar la siguiente interfaz visual:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Data.SqlClient;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
publicpartialclassbaja:
System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,
EventArgse)
{
}
protectedvoidButton1_Click(object
sender,EventArgse)
{
strings=
System.Configuration.ConfigurationManager.Con
nectionStrings["cadenaconexion1"].ConnectionS
tring;
SqlConnectionconexion=new
SqlConnection(s);
conexion.Open();
SqlCommandcomando=new
SqlCommand("deletefromusuarioswhere
nombre='"+this.TextBox1.Text+"'",
conexion);
intcantidad=
comando.ExecuteNonQuery();
if(cantidad==1)
this.Label1.Text="Seborrel
usuario";
else
this.Label1.Text="Noexisteun
usuariocondichonombre";
conexion.Close();
}
}
Modificaciones
Por ltimo implementaremos la modificacin de datos. Seleccionamos del
Explorador de soluciones la pgina modificacion.aspx y procedemos a
elaborar la siguiente interfaz visual:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Data.SqlClient;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
publicpartialclassmodificacion:
System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,
EventArgse)
{
}
protectedvoidButton1_Click(object
sender,EventArgse)
{
strings=
System.Configuration.ConfigurationManager.Con
nectionStrings["cadenaconexion1"].ConnectionS
tring;
SqlConnectionconexion=new
SqlConnection(s);
conexion.Open();
SqlCommandcomando=new
SqlCommand("selectnombre,clave,mailfrom
usuarios"+
"wherenombre='"+
this.TextBox1.Text+"'",conexion);
SqlDataReaderregistro=
comando.ExecuteReader();
if(registro.Read())
{
this.TextBox2.Text=
registro["clave"].ToString();
this.TextBox3.Text=
registro["mail"].ToString();
}
else
this.Label1.Text="Noexisteun
usuariocondichonombre";
conexion.Close();
}
protectedvoidButton2_Click(object
sender,EventArgse)
{
strings=
System.Configuration.ConfigurationManager.Con
nectionStrings["cadenaconexion1"].ConnectionS
tring;
SqlConnectionconexion=new
SqlConnection(s);
conexion.Open();
SqlCommandcomando=new
SqlCommand("updateusuariosset"+
"clave='"+
this.TextBox2.Text+
"',mail='"+
this.TextBox3.Text+
"'wherenombre='"+
this.TextBox1.Text+"'",conexion);
intcantidad=
comando.ExecuteNonQuery();
if(cantidad==1)
this.Label2.Text="Datos
Modificados";
else
this.Label2.Text="Noexisteel
usuario";
conexion.Close();
}
}
El botn ?Buscar? hace lo mismo que vimos en la consulta. Luego
cuando se presiona el botn ?Modificar? procedemos a hacer un update
de la tabla usuarios con los datos cargados en los TextBox.