Número 50julio-AGOSTO 2008

Visual Basic•C#•ASP.NET•ADO.NET•AJAXSilverlight.NET Framework

dotNetManía

Revista dedicada a los profesionales de la plataforma .NET

Menú

Inicio

Números publicados

Libros

Próximo número (nº51)

Autores

¿Qué es dotNetManía?

Garantía de satisfacción

Contactar

Pedidos

Suscripciones

Renovaciones

Libros

Noticias dnm

Alhambra-Eidos llega a un acuerdo con dotNetManía para la difusión conjunta de su oferta formativa. Leer más...


Disponibles los primeros 28 números de dotNetManía en formato PDF y de libre distribución. Leer más... 

Patrocinadores

Patrocinador Oro
Microsoft

Patrocinadores Plata
Alhambra-Eidos
Solid Quality Mentors

Patrocinadores Bronce
Raona
Plain Concepts
Krasis
ABOX 

 

 

 

Nº 12 Enero 2005 Creación y utilización de servicios Web desde ASP.NET Material de apoyo


Por Ángel Esteban


Código fuente
 

Imports System.Web.Services

 

<System.Web.Services.WebService(Namespace:="http://tempuri.org/Art/Service1")> _

Public Class Service1

    Inherits System.Web.Services.WebService

 

#Region " Código generado por el Diseñador de servicios Web "

 

    Public Sub New()

        MyBase.New()

 

        'El Diseñador de servicios Web requiere esta llamada.

        InitializeComponent()

 

        'Agregar su propio código de inicialización después de llamar a

        'InitializeComponent()

 

    End Sub

 

    'Requerido por el Diseñador de servicios Web

    Private components As System.ComponentModel.IContainer

 

    'NOTE: el Diseñador de servicios Web requiere el siguiente procedimiento

    'Puede modificarse utilizando el Diseñador de servicios Web.

    'No lo modifique con el editor de código.

    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        components = New System.ComponentModel.Container

    End Sub

 

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

        'CODEGEN: el Diseñador de servicios Web requiere este procedimiento

        'No lo modifique con el editor de código.

        If disposing Then

            If Not (components Is Nothing) Then

                components.Dispose()

            End If

        End If

        MyBase.Dispose(disposing)

    End Sub

 

#End Region

 

    ' EJEMPLO DE SERVICIO WEB

    ' El servicio de ejemplo HelloWorld() devuelve la cadena Hello World.

    ' Para generar, quite los comentarios de las siguientes líneas y, a

    ' continuación, guarde y genere el proyecto.

    ' Para probar este servicio Web, compruebe que la página de inicio es el

    ' archivo .asmx y presione F5.

    '

    '<WebMethod()> Public Function HelloWorld() As String

    ' HelloWorld = "Hello World"

    ' End Function

 

End Class

 

Fuente 1.

 

<%@ WebService Language="vb" Codebehind="ServicioWeb.asmx.vb"_

    Class="Art.ServicioWeb" %>

Fuente 2.

 

<WebMethod(Description:="el clásico Hola Mundo")> _

Public Function HolaMundo() As String

    HolaMundo = "Hola Mundo"

End Function

 

Fuente 3.

 

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb"

    Inherits="Art.WebForm1"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML>

  <HEAD>

        <title>WebForm1</title>

        <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">

        <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">

        <meta name="vs_defaultClientScript" content="JavaScript">

        <meta name="vs_targetSchema"

              content="http://schemas.microsoft.com/intellisense/ie5">

  </HEAD>

  <body MS_POSITIONING="GridLayout">

    <form id="Form1" method="post" runat="server">

       <asp:Button id="btnHolaMundo"

                   style="Z-INDEX: 101; LEFT: 48px;POSITION: absolute; TOP: 32px"

                   runat="server" Text="Hola Mundo" Width="104px"  Height="24px">

       </asp:Button>

       <asp:Label id="lbHolaMundo"

                  style="Z-INDEX: 102; LEFT: 48px; POSITION: absolute; TOP: 72px"

                  runat="server" Width="96px">

       </asp:Label>

    </form>

  </body>

</HTML>

 

Fuente 4.

 

Public Class WebForm1

    Inherits System.Web.UI.Page

 

    Private Sub btnHolaMundo_Click( ByVal sender As System.Object, _

                                    ByVal e As System.EventArgs) _

                Handles btnHolaMundo.Click

        lbHolaMundo.Text = MiServicio.HolaMundo()

    End Sub

 

End Class

 

Fuente 5.

 

<WebMethod(Description:="devuelve el nombre de la máquina del servidor")> _

 Public Function NombreServidor() As String

    Return Server.MachineName

End Function

 

<WebMethod(Description:="devuelve la hora del servidor")> _

Public Function HoraServidor() As String

    Return Context.Timestamp.TimeOfDay.ToString()

End Function

 

Fuente 6.

 

Private Sub btnDatosServidor_Click(ByVal sender As System.Object, _

                                   ByVal e As System.EventArgs) _

            Handles btnDatosServidor.Click

    lbDatosServidor.Text = MiServicio.NombreServidor() & " ----- " & _

                           MiServicio.HoraServidor()

End Sub

 

Fuente 7.

 

<WebMethod(Description:="devuelve el cubo de un entero")> _

Public Function Cubo(ByVal valor As Integer) As Double

    Return Math.Pow(valor, 3)

End Function

 

Fuente 8.

 

Private Sub btnCubo_Click(ByVal sender As System.Object, _

                          ByVal e As System.EventArgs) _

            Handles btnCubo.Click

    lbResultado.Text = MiServicio.Cubo(txtValor.Text)

End Sub

 

Fuente 9.

 

Imports System.Data

Imports System.Data.SqlClient

 

Public Class Clase

 

    Public Function DevuelveEmpleados() As DataSet

        Dim conexion As SqlConnection = New SqlConnection( _

                     "server=(local);database=northwind;uid=sa;pwd=xxx")

        Dim adapterEmpleados As SqlDataAdapter = New SqlDataAdapter( _

                     "select firstname, lastname from Employees", conexion)

        Dim ds As DataSet = New DataSet

        conexion.Open()

        adapterEmpleados.Fill(ds, "Empleados")

        conexion.Close()

        Return ds

    End Function

 

End Class

 

Fuente 10.

 

<WebMethod(Description:="devuelve a través de un clase un DataSet de empleados")> _

Public Function DevuelveEmpleados() As DataSet

    Dim MiObjeto As New Clase

    Return MiObjeto.DevuelveEmpleados()

End Function

 

Fuente 11.

 

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb"

    Inherits="Art.WebForm1"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML>

 <HEAD>

  <title>WebForm1</title>

  <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">

  <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">

  <meta name="vs_defaultClientScript" content="JavaScript">

  <meta name="vs_targetSchema"

        content= "http://schemas.microsoft.com/intellisense/ie5">

 </HEAD>

 <body MS_POSITIONING="GridLayout">

  <form id="Form1" method="post" runat="server">

    <asp:Button id="btnSQL"

                style="Z-INDEX: 105; LEFT: 32px; POSITION: absolute; TOP: 24px"

                runat="server" Text="Datos SQL">

    </asp:Button>

   

    <asp:DataGrid id="dgDatos"

                  style="Z-INDEX: 106; LEFT: 144px; POSITION: absolute; TOP: 24px"

                  runat="server">

    </asp:DataGrid>

  </form>

 </body>

</HTML>

 

Fuente 12.

 

Private Sub btnSQL_Click(ByVal sender As System.Object, _

                         ByVal e As System.EventArgs) _

            Handles btnSQL.Click

    dgDatos.DataSource = MiServicio.DevuelveEmpleados()

    dgDatos.DataBind()

End Sub

 

Fuente 13.

Volver
 


dotNetManía es una revista editada por Netalia. Más información.