Servicio ASMX con SQL – Dedicado a el canal Mario Briceño
Antes de comenzar gracias a todos los que me seguis en youtube o aqui , a traves del portal de soulsmakers.
Empecemos.
Cosas que os haran falta, la dll de adodb, lo podeis hacer con oledb, en este ejemplo uso ado.
1 crea el proyecto para el web service y agrega como referencia adodb para el acceso a bbdd
2 ten preparado la cadena de conexion .
3 crea un servicio web y aqui te dejo el codigo que se muestra en el video del web service.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
Imports System.Web.Services Imports System.Web.Services.Protocols Imports System.ComponentModel ' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. ' <System.Web.Script.Services.ScriptService()> _ <System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _ <System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _ <ToolboxItem(False)> _ Public Class ASMXSQL Inherits System.Web.Services.WebService Dim BBDD As ADODB.Connection <WebMethod()> Public Function DevolverDatosTabla1() As DataTable Dim str As String = "select * from tabla" Dim dt As New DataTable Dim da As New OleDb.OleDbDataAdapter PrepararCadena() BBDD.Open() da.Fill(dt, BBDD.Execute(str)) BBDD.Close() dt.TableName = "Tabla" dt.AcceptChanges() Return dt End Function Private Function PrepararCadena() BBDD = New ADODB.Connection BBDD.ConnectionString = "AQUI LA CADENA DE CONEXION A BBDD" End Function End Class |
Compila y e inicia la depuracion para que arranque el iis y te deje el servicio arrancado para poder añadirlo como indica el video.
4 crea un proyecto web.
5 añade como referencia de servico el servicio publicado en tu iis en local.
6 crea una pagina en blanco y añade un gridview, aqui el codigo de la webform.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Prueba.aspx.vb" Inherits="WebParaConsumeAsmx.Prueba" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server"> </asp:GridView> </div> </form> </body> </html> |
7 en el evento load añade el codigo que aparece aqui. Deberas cambia el nombre del servicio a el que has echo referencia por el que has puesto tu, yo te dejo el del video.
1 2 3 4 5 6 7 8 9 10 11 |
Public Class Prueba Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim MiPrueba As New PruebaSQL.ASMXSQLSoapClient Dim DtConDatos As DataTable = MiPrueba.DevolverDatosTabla1 Me.GridView1.DataSource = DtConDatos Me.GridView1.DataBind() End Sub End Class |
y esto es todo, Espero que te sirva. un saludo.