Conexión
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
namespace WebAplicacion01
{
public class Conexion02
{
String cadena;
public SqlConnection ObtenerConexion()
{
cadena = "Server=localhost;Database=SISTEMA0001;User id=sa;Password=123";
return new SqlConnection(cadena);
}
}
}
ServicioWeb
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;
namespace WebAplicacion01
{
[WebService(Namespace = "http://localhost/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class WebServicio01 : System.Web.Services.WebService
{
[WebMethod]
public DataTable ListadoAlumnos()
{
Conexion02 con = new Conexion02();
SqlConnection cn = con.ObtenerConexion();
try
{
using(SqlCommand comandolistado=new SqlCommand("usp_listado_alumnos", cn))
{
comandolistado.CommandType = CommandType.StoredProcedure;
using (SqlDataAdapter adaptador = new SqlDataAdapter(comandolistado))
{
DataTable dtlistado = new DataTable("Listado");
adaptador.Fill(dtlistado);
return dtlistado;
}
}
}
catch (Exception e1) {
throw new Exception("Error En El Listado De Datos : " + e1.Message);
}
finally
{
if (cn.State == ConnectionState.Open)
{
cn.Close();
}
}
}
[WebMethod]
public DataTable BusquedaAlumnoCodigo(string codigoAlumno)
{
Conexion02 con = new Conexion02();
SqlConnection cn = con.ObtenerConexion();
try
{
using (SqlCommand comandoBusqueda = new SqlCommand("usp_busqueda_alumnos", cn))
{
comandoBusqueda.CommandType = CommandType.StoredProcedure;
comandoBusqueda.Parameters.AddWithValue("@xcodalumno", codigoAlumno);
using (SqlDataAdapter adaptador = new SqlDataAdapter(comandoBusqueda))
{
DataTable dtBusqueda = new DataTable("Busqueda");
adaptador.Fill(dtBusqueda);
return dtBusqueda;
}
}
}
catch (Exception e)
{
throw new Exception("Error en la búsqueda de datos: " + e.Message);
}
finally
{
if (cn.State == ConnectionState.Open)
{
cn.Close();
}
}
}
}
}
ServicioWeb
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebListado.aspx.cs"
Inherits="WebAplicacion01.WebListado" %>
<!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>LISTADO DE DATOS</title>
<link rel="stylesheet" type="text/css" href="Estilo01.css" />
<script type="text/javascript" src="Script01.js"></script>
</head>
<body>
<form id="form1" runat="server" >
<center>
<h1 class="formatoh1">LISTADO Y BUSQUEDA DE ALUMNOS</h1>
</center>
<center>
<div class="formatotabla">
<table >
<tr>
<td>CODIGO</td>
<td>
<asp:TextBox ID="txtcodigo" CssClass="centrado" runat="server" BackColor="Yellow"
BorderColor="#000066" BorderStyle="Double" BorderWidth="3px" Font-Bold="True" Font-Size="X-Large"
ForeColor="Red" MaxLength="7"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnbuscar" runat="server" BackColor="#000066" Font-Bold="True"
Font-Size="X-Large" ForeColor="White" Text="BUSCAR" OnClick="btnbuscar_Click"
OnClientClick="return validarBusqueda();"/>
</td>
</tr>
</table>
</div>
<div class="separator"></div>
<div class="formato">
<asp:GridView ID="gvwlistado" runat="server" Height="307px" Width="1606px">
</asp:GridView>
</div>
</center>
<div class="separator"></div>
<center>
<div class="formato">
<asp:Button ID="btnlistado" runat="server" BackColor="#000066" Font-Bold="True" Font-
Size="X-Large" ForeColor="White" Text="LISTADO" OnClick="btnlistado_Click"/>
</div>
</center>
</form>
</body>
</html>

conexionsqlserverC#codigointermedio.docx

  • 1.
    Conexión using System; using System.Collections.Generic; usingSystem.Linq; using System.Web; using System.Data.SqlClient; namespace WebAplicacion01 { public class Conexion02 { String cadena; public SqlConnection ObtenerConexion() { cadena = "Server=localhost;Database=SISTEMA0001;User id=sa;Password=123"; return new SqlConnection(cadena); } } } ServicioWeb using System; using System.Data; using System.Data.SqlClient; using System.Web.Services; namespace WebAplicacion01 { [WebService(Namespace = "http://localhost/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] public class WebServicio01 : System.Web.Services.WebService { [WebMethod] public DataTable ListadoAlumnos() { Conexion02 con = new Conexion02(); SqlConnection cn = con.ObtenerConexion(); try { using(SqlCommand comandolistado=new SqlCommand("usp_listado_alumnos", cn)) { comandolistado.CommandType = CommandType.StoredProcedure; using (SqlDataAdapter adaptador = new SqlDataAdapter(comandolistado)) { DataTable dtlistado = new DataTable("Listado"); adaptador.Fill(dtlistado); return dtlistado; } } } catch (Exception e1) { throw new Exception("Error En El Listado De Datos : " + e1.Message);
  • 2.
    } finally { if (cn.State ==ConnectionState.Open) { cn.Close(); } } } [WebMethod] public DataTable BusquedaAlumnoCodigo(string codigoAlumno) { Conexion02 con = new Conexion02(); SqlConnection cn = con.ObtenerConexion(); try { using (SqlCommand comandoBusqueda = new SqlCommand("usp_busqueda_alumnos", cn)) { comandoBusqueda.CommandType = CommandType.StoredProcedure; comandoBusqueda.Parameters.AddWithValue("@xcodalumno", codigoAlumno); using (SqlDataAdapter adaptador = new SqlDataAdapter(comandoBusqueda)) { DataTable dtBusqueda = new DataTable("Busqueda"); adaptador.Fill(dtBusqueda); return dtBusqueda; } } } catch (Exception e) { throw new Exception("Error en la búsqueda de datos: " + e.Message); } finally { if (cn.State == ConnectionState.Open) { cn.Close(); } } } } } ServicioWeb <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebListado.aspx.cs" Inherits="WebAplicacion01.WebListado" %> <!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>LISTADO DE DATOS</title> <link rel="stylesheet" type="text/css" href="Estilo01.css" /> <script type="text/javascript" src="Script01.js"></script> </head> <body> <form id="form1" runat="server" >
  • 3.
    <center> <h1 class="formatoh1">LISTADO YBUSQUEDA DE ALUMNOS</h1> </center> <center> <div class="formatotabla"> <table > <tr> <td>CODIGO</td> <td> <asp:TextBox ID="txtcodigo" CssClass="centrado" runat="server" BackColor="Yellow" BorderColor="#000066" BorderStyle="Double" BorderWidth="3px" Font-Bold="True" Font-Size="X-Large" ForeColor="Red" MaxLength="7"></asp:TextBox> </td> <td> <asp:Button ID="btnbuscar" runat="server" BackColor="#000066" Font-Bold="True" Font-Size="X-Large" ForeColor="White" Text="BUSCAR" OnClick="btnbuscar_Click" OnClientClick="return validarBusqueda();"/> </td> </tr> </table> </div> <div class="separator"></div> <div class="formato"> <asp:GridView ID="gvwlistado" runat="server" Height="307px" Width="1606px"> </asp:GridView> </div> </center> <div class="separator"></div> <center> <div class="formato"> <asp:Button ID="btnlistado" runat="server" BackColor="#000066" Font-Bold="True" Font- Size="X-Large" ForeColor="White" Text="LISTADO" OnClick="btnlistado_Click"/> </div> </center> </form> </body> </html>