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中定义二个功能调用:
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 |
9. 保存上述的修改,一个简单的操作Sql Server数据库的Web Service就完成了,此时单击快捷键F5,此Web Service就开始运行,并可以对外提供服务了。具体如图02所示:

图02Web Service提供服务是的界面
Service1.asmx.vb的代码清单如下:
Imports System.Web.Services Imports System.Data.SqlClient _ Public Class Service1 Inherits 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 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 #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 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 键。 ' ' Public Function HelloWorld ( ) As String ' HelloWorld = "Hello World" ' End Function End Class |
下面就来介绍Visual Basic .Net中使用这个Web Service提供的服务来更新数据库的实现方法。