vbnet编程实例教程的简单介绍( 二 )


SOAP 是 Simple Object Access Protocol 的缩写 即 简单对象访问协议 SOAP是一种消息传递的协议 它规定了Web Services之间传递信息的方式
三 本文章的程序设计 调试和运行的环境
( ) 微软公司视窗 中文企业版
( ) Visual Studio Net 企业构建版 Net FrameWork SDK 版本号
( ) IIS服务启动
四 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的具体步骤
启动Visual Studio Net
选择菜单「文件」|「新建」|「项目」后 弹出「新建项目」对话框
将「项目类型」设置为「Visual Basic项目」
将「模板」设置为「ASP NET Web 服务」
在「位置」的文本框中输入//localhost/UpdateDataWebService 后 单击「确定」按钮 这样在Visual Studio Net就会计算机Internet信息服务的默认目录中创建一个名称为 UpdateDataWebService 文件夹 里面存放的是此项目的文件 具体如图 所示
图 创建Web Service项目对话框
选中「解决方案资源管理器」中的 Service a *** x 文件 单击鼠标右键 在弹出的菜单中选择「查看代码」 则进入Service a *** x vb的编辑界面
在Service a *** x……vb的首部 在导入命名空间的代码区中添加下列代码 下列代码作用是导入命名空间System Data SqlClient
Imports System Data SqlClient
在Service a *** x……vb文件的 Public Class Service 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 dsEnd FunctionWebMethod ( )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 dsEnd Function
保存上述的修改 一个简单的操作Sql Server数据库的Web Service就完成了 此时单击快捷键F 此Web Service就开始运行 并可以对外提供服务了 具体如图 所示:
图 :Web Service提供服务是的界面
Service a *** x vb的代码清单如下:
Imports System Web ServicesImports System Data SqlClientWebService ( Namespace := )_Public Class Service Inherits System Web Services WebServiceWebMethod ( )Public Function Binding ( ) As DataSet Modify this Connection string to use your SQL Server and log onDim 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 dsEnd FunctionWebMethod ( )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 dsEnd Function#Region Web 服务设计器生成的代码 Public Sub New ( ) MyBase New ( )该调用是 Web 服务设计器所必需的InitializeComponent ( )在 InitializeComponent ( ) 调用之后添加您自己的初始化代码End Sub Web 服务设计器所必需的Private ponents As System ComponentModel IContainer注意 以下过程是 Web 服务设计器所必需的可以使用 Web 服务设计器修改此过程不要使用代码编辑器修改它System Diagnostics DebuggerStepThrough ( )Private Sub InitializeComponent ( ) ponents = New System ComponentModel Container ( )End SubProtected Overloads Overrides Sub Dispose ( ByVal disposing As Boolean ) CODEGEN: 此过程是 Web 服务设计器所必需的 不要使用代码编辑器修改它 If disposing Then If Not ( ponents Is Nothing ) Thenponents Dispose ( ) End IfEnd IfMyBase Dispose ( disposing )End Sub#End Region Web 服务示例 HelloWorld ( ) 示例服务返回字符串 Hello World 若要生成项目 请取消注释以下行 然后保存并生成项目 若要测试此 Web 服务 请确保 a *** x 文件为起始页 并按 F 键 WebMethod ( )Public Function HelloWorld ( ) As String HelloWorld = Hello World End FunctionEnd Class

推荐阅读