You are on page 1of 10

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;

namespace DATOS
{
public class ACCESOADATOS
{
public static DataTable mostrarUsuarios()
{
SqlCommand comando =
METODODATOS.crearcomandoproc("UspListaUsuario");
return METODODATOS.ejecutarselect(comando);
}
}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DATOS
{
public class configuration
{
static string cadenaconexion =
@"data source=DESKTOP-SD183BQ\MSSQLSERVER01;initial
catalog=DBPRUEBA;integrated security=true";
public static string CadenaConexion
{
get
{
return cadenaconexion;
}
}

}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;

namespace DATOS
{
public class METODODATOS
{
public static SqlCommand Crearcomando()
{
string cadenaconexion = configuration.CadenaConexion;
SqlConnection conexion = new SqlConnection();
conexion.ConnectionString = cadenaconexion;
SqlCommand comando = new SqlCommand();
return comando;
}
public static SqlCommand crearcomandoproc(string proc)
{
string cadenaconexion = configuration.CadenaConexion;
SqlConnection conexion = new SqlConnection(cadenaconexion);
SqlCommand comando = new SqlCommand(proc, conexion);
comando.CommandType = CommandType.StoredProcedure;
return comando;
}
public static int ejecutarcomando(SqlCommand comando)
{
try
{
comando.Connection.Open();
return comando.ExecuteNonQuery();

}
catch
{
throw;
}
finally
{
comando.Connection.Dispose();
comando.Connection.Close();

}
}
public static DataTable ejecutarselect(SqlCommand comando)
{
DataTable tabla = new DataTable();
try
{
comando.Connection.Open();
SqlDataAdapter adaptador = new SqlDataAdapter();
adaptador.SelectCommand = comando;
adaptador.Fill(tabla);
}

catch
{
throw;
}
finally
{
comando.Connection.Close();
}
return tabla;
}
}
}

using DATOS;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Negocio
{
public class CONSULTADATOS
{
public static DataTable mostrarUsuarios()
{
return ACCESOADATOS.mostrarUsuarios();
}
}
}

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="aplicacion.aspx.cs"


Inherits="VISTA.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div style="height: 200px">

<table>

<asp:Button ID="Button1" runat="server" Text="Mostar Usuarios"


Width="121px" OnClick="Button1_Click" style="margin-top: 0px" />

<asp:GridView ID="GridView1" runat="server"


AutoGenerateColums="false"
>
<Columns>
<asp:BoundField headertext="Identificacion"
DataField="CodUsu" />
<asp:BoundField headertext="Nombre" DataField="NombreUsu"
/>
<asp:BoundField headertext="Apellido Paterno"
DataField="ApellidoPatUsu" />
<asp:BoundField headertext="Apellido Materno"
DataField="ApellidoPatUsu" />
<asp:BoundField headertext="Dni" DataField="DniUsu" />
<asp:BoundField headertext="Profesion" DataField="Perfil"
/>

</Columns>

</asp:GridView>
</table>
</div>
</form>

</body>
</html>

Boton del Formulario Web “Mostrar”


using Negocio;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace VISTA
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

protected void Button1_Click(object sender, EventArgs e)


{
GridView1.DataSource = CONSULTADATOS.mostrarUsuarios();
GridView1.DataBind();
}
}
}

You might also like