vb.net基础教程 vbnet using( 三 )


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
下面就来介绍Visual Basic Net中使用这个Web Service提供的服务来更新数据库的实现方法
五 在Visual Basic Net调用Web Service提供的服务:
当Web Service已经处于对外提供服务状态 Visual Basic Net就可以通过HTTP 调用 来使用这些服务了 当然前提是要了解Web Service对外提供服务所对应的URL 当了解到Web Service对应的URL后 Visual Basic Net就像是使用本地的类库一样使用Web Service中提供的各种功能 所以有些人说 Web Service从实质上说 就是通过HTTP调用远程组件的一种方式 在Visual Basic Net具体实现加入Web Service可参阅下面步骤中的第七步
在下面介绍的这个数据库应用程序是通过使用上面的Web Service中提供的 Binding 服务 对程序中DataGrid组件实现数据绑定 提供使用Web Service中提供的 Update 服务 通过程序中的DataGrid来修改数据库 下面就是Visual Basic Net中使用Web Service提供服务来编写数据库应用程序的具体步骤 :
启动Visual Studio Net
选择菜单【文件】|【新建】|【项目】后 弹出【新建项目】对话框
将【项目类型】设置为【Visual Basic项目】
将【模板】设置为【Windows应用程序】
在【名称】文本框中输入【TestWebService】
在【位置】的文本框中输入【E:\VS NET项目】 然后单击【确定】按钮 这样在 E:\VS NET项目 中就产生了名称为 TestWebService 文件夹 里面存放的就是TestWebService项目的所有文件
选择【解决方案资源管理器】|【引用】后 单击鼠标右键 在弹出的菜单中选择【添加Web 引用】 在弹出的【添加Web引用】对话框中的【地址】文本框中输入 后 单击回车键后 可得图 所示界面 单击图 中【添加引用】按钮 则在【TestWebService】项目中加入了Web引用 请注意 就是上面完成的Web Service对外提供服务的URL地址 具体可参阅图 所示:
图 :在【TestWebService】添加Web Service提供的服务

推荐阅读