詳解VB.NET實現Web Service
VB.NET還是比較常用的,于是我研究了一下VB.NET實現Web Service,在這里拿出來和大家分享一下,希望對大家有用。
VB.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。下面就來用VB.NET實現一個Web Service,此Web Service和數據庫相關,數據庫類型選用的是SqlServer。此Web Service提供了二個函數功能調用,其一名稱為Binding,用以實現數據綁定,其二名稱為Update,用以更新數據庫中的數據。
以下就是VB.NET實現Web Service的具體步驟:
1. 啟動Visual Studio .NET。
2. 選擇菜單【文件】|【新建】|【項目】后,彈出【新建項目】對話框。
3. 將【項目類型】設置為【VB項目】。
4. 將【模板】設置為【ASP.NET Web 服務】。
5. 在【位置】的文本框中輸入"http://localhost/UpdateDataWebService"后,單擊【確定】按鈕,這樣在Visual Studio .NET就會計算機Internet信息服務的默認目錄中創建一個名稱為"UpdateDataWebService"文件夾,里面存放的是此項目的文件。
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中定義二個功能調用:
- 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就開始運行,并可以對外提供服務了。
- 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
以上介紹VB.NET實現Web Service
【編輯推薦】