科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道基础软件VB.Net编程实现Web Service的基础

VB.Net编程实现Web Service的基础

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

本文简要介绍一下Web Service的相关知识,以及使用VB.Net实现的方法和步骤。

作者:马金虎 来源:天极开发 2007年11月9日

关键字:

  • 评论
  • 分享微博
  • 分享邮件
四.Visual Basic .Net实现Web Service:

  .Net 的大的推动了Web Service的发展,而Visual Studio .Net的出现又极大的推动了Web Service的的广泛应用。在Visual Studio .Net推出之前,编写一个Web Service是一项非常复杂的工作,同样调用这个Web Service也十分麻烦。由于Visual Studio .Net对Web Service提供了较强的支持,很多细致、烦杂的工作都由Visual Studio .Net自动完成了。这样就使得上述工作变得非常简单。甚至不了解Web Service和其相关的标准、协议,也可以使用Visual Studio .Net编写Web Service,并使用这个Web Service。下面就来用Visual Basic .Net实现一个Web Service,此Web Service和数据库相关,数据库类型选用的是Sql Server。此Web Service提供了二个函数功能调用,其一名称为Binding,用以实现数据绑定,其二名称为Update,用以更新数据库中的数据。

  以下就是Visual Basic .Net实现此Web Service的具体步骤:

  1. 启动Visual Studio .Net。

  2. 选择菜单【文件】|【新建】|【项目】后,弹出【新建项目】对话框。

  3. 将【项目类型】设置为【Visual Basic项目】。

  4. 将【模板】设置为【ASP.NET Web 服务】。

  5. 在【位置】的文本框中输入"http://localhost/UpdateDataWebService"后,单击【确定】按钮,这样在Visual Studio .Net就会计算机Internet信息服务的默认目录中创建一个名称为"UpdateDataWebService"文件夹,里面存放的是此项目的文件。具体如图01所示:


图01:创建Web Service项目对话框

  6. 选中【解决方案资源管理器】中的"Service1.asmx"文件,单击鼠标右键,在弹出的菜单中选择【查看代码】,则进入Service1.asmx.vb的编辑界面。

  7. 在Service1.asmx..vb的首部,在导入命名空间的代码区中添加下列代码,下列代码作用是导入命名空间System.Data.SqlClient:

Imports System.Data.SqlClient

  8. 在Service1.asmx..vb文件的"Public Class Service1 Inherits System.Web.Services.WebService"代码后,添加下列代码,下列代码是在Web Service中定义二个功能调用:

<WebMethod ( ) > Public Function Binding ( ) As DataSet
 Dim con As New SqlConnection ( "Server = localhost ; uid = sa ; pwd = ; database = northwind" )
 Dim daCust As New SqlDataAdapter ( "Select * From Customers" , con )
 Dim ds As New DataSet ( )
 daCust.Fill( ds , "Cust" )
 Return ds
End Function
<WebMethod ( ) > Public Function Update ( ByVal ds As DataSet ) As DataSet
 Dim con As New SqlConnection ( "Server = localhost ; uid = sa ; pwd = ; database = northwind " )
 Dim daCust As New SqlDataAdapter ( "Select * From Customers" , con )
 Dim cbCust As New SqlCommandBuilder ( daCust )
 daCust.Update ( ds , "Cust" )
 Return ds
End Function

  9. 保存上述的修改,一个简单的操作Sql Server数据库的Web Service就完成了,此时单击快捷键F5,此Web Service就开始运行,并可以对外提供服务了。具体如图02所示:


图02:Web Service提供服务是的界面

  Service1.asmx.vb的代码清单如下:

Imports System.Web.Services
Imports System.Data.SqlClient
<WebService ( Namespace := "http://tempuri.org/" ) > _
Public Class Service1
Inherits System.Web.Services.WebService
<WebMethod ( ) > Public Function Binding ( ) As DataSet
'Modify this Connection string to use your SQL Server and log on.
 Dim con As New SqlConnection ( "Server=localhost;uid=sa;pwd=;database=northwind" )
 Dim daCust As New SqlDataAdapter ( "Select * From Customers" , con )
 Dim ds As New DataSet ( )
 daCust.Fill ( ds , "Cust" )
 Return ds
End Function
<WebMethod ( ) > Public Function Update ( ByVal ds As DataSet ) As DataSet
 Dim con As New SqlConnection ( "Server=localhost;uid=sa;pwd=;database=northwind" )
 Dim daCust As New SqlDataAdapter ( "Select * From Customers" , con )
 Dim cbCust As New SqlCommandBuilder ( daCust )
 daCust.Update ( ds , "Cust" )
 Return ds
End Function
#Region " Web 服务设计器生成的代码 "
Public Sub New ( )
 MyBase.New ( )
 '该调用是 Web 服务设计器所必需的。
 InitializeComponent ( )
 '在 InitializeComponent ( ) 调用之后添加您自己的初始化代码
End Sub
'Web 服务设计器所必需的
Private components As System.ComponentModel.IContainer
 '注意:以下过程是 Web 服务设计器所必需的
 '可以使用 Web 服务设计器修改此过程。
 '不要使用代码编辑器修改它。
 <System.Diagnostics.DebuggerStepThrough ( ) > Private Sub InitializeComponent ( )
 components = New System.ComponentModel.Container ( )
End Sub
Protected Overloads Overrides Sub Dispose ( ByVal disposing As Boolean )
'CODEGEN: 此过程是 Web 服务设计器所必需的
'不要使用代码编辑器修改它。
If disposing Then
 If Not ( components Is Nothing ) Then
  components.Dispose ( )
 End If
End If
MyBase.Dispose ( disposing )
End Sub
#End Region
' Web 服务示例
' HelloWorld ( ) 示例服务返回字符串 Hello World。
' 若要生成项目,请取消注释以下行,然后保存并生成项目。
' 若要测试此 Web 服务,请确保 .asmx 文件为起始页
' 并按 F5 键。
'
'<WebMethod ( ) > Public Function HelloWorld ( ) As String
' HelloWorld = "Hello World"
' End Function
End Class

  下面就来介绍Visual Basic .Net中使用这个Web Service提供的服务来更新数据库的实现方法。
    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章