.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和数据库相关,数据库类型选用的是SqlServer。此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所示:
8. 在Service1.asmx..vb文件的"Public Class Service1 Inherits System.Web.Services.WebService"代码后,添加下列代码,下列代码是在Web Service中定义二个功能调用:
以下为引用的内容:
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
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作者: jinlj 时间: 2008-10-28 13:34 标题: VB.Net实现Web Service的基础 . 保存上述的修改,一个简单的操作Sql Server数据库的Web Service就完成了,此时单击快捷键F5,此Web Service就开始运行,并可以对外提供服务了。具体如图02所示:
图02Web Service提供服务是的界面
Service1.asmx.vb的代码清单如下:
以下为引用的内容:
Imports System.Web.ServicesImports System.Data.SqlClient _Public Class Service1Inherits System.Web.Services.WebService 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 dsEnd Function 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 components As System.ComponentModel.IContainer '注意:以下过程是 Web 服务设计器所必需的 '可以使用 Web 服务设计器修改此过程。 '不要使用代码编辑器修改它。 ) > Private Sub InitializeComponent ( ) components = New System.ComponentModel.Container ( ) End SubProtected Overloads Overrides Sub Dispose ( ByVal disposing As Boolean ) 'CODEGEN: 此过程是 Web 服务设计器所必需的'不要使用代码编辑器修改它。If disposing Then If Not ( components Is Nothing ) Thencomponents.Dispose ( ) End IfEnd IfMyBase.Dispose ( disposing ) End Sub#End Region' Web 服务示例' HelloWorld ( ) 示例服务返回字符串 Hello World。' 若要生成项目,请取消注释以下行,然后保存并生成项目。 ' 若要测试此 Web 服务,请确保 .asmx 文件为起始页' 并按 F5 键。'' Public Function HelloWorld ( ) As String' HelloWorld = "Hello World"' End FunctionEnd Class
10. 把Visual Studio .Net的当前窗口切换到Form1.vb的代码编辑窗口,并用下列代码替换Form1.vb中的Button1的Click事件对应的处理代码,下列代码功能是使用Web Service中提供的"Binding"服务对DataGrid组件实现数据绑定:
以下为引用的内容:
Private Sub Button1_Click ( ByVal sender As System.Object , ByVal e As System.EventArgs ) Handles Button1.Click Dim MyService As New localhost.Service1 ( ) DataGrid1.DataSource = MyService.Binding ( ) DataGrid1.DataMember = "Cust"End Sub
Private Sub Button2_Click (ByVal sender As System.Object , ByVal e As System.EventArgs ) Handles Button2.Click Dim MyService As New localhost.Service1 ( ) Dim ds As DataSet = DataGrid1.DataSource Dim dsChanges As DataSet = ds.GetChanges ( ) If Not ( dsChanges Is Nothing ) Thends.Merge ( MyService.Update ( dsChanges ) , True ) End IfEnd Sub