詳解VB.NET實(shí)現(xiàn)Web Service
VB.NET還是比較常用的,于是我研究了一下VB.NET實(shí)現(xiàn)Web Service,在這里拿出來(lái)和大家分享一下,希望對(duì)大家有用。
VB.NET實(shí)現(xiàn)Web Service
.NET的大的推動(dòng)了Web Service的發(fā)展,而Visual Studio .NET的出現(xiàn)又極大的推動(dòng)了Web Service的的廣泛應(yīng)用。在Visual Studio .NET推出之前,編寫一個(gè)Web Service是一項(xiàng)非常復(fù)雜的工作,同樣調(diào)用這個(gè)Web Service也十分麻煩。由于Visual Studio .NET對(duì)Web Service提供了較強(qiáng)的支持,很多細(xì)致、煩雜的工作都由Visual Studio .NET自動(dòng)完成了。這樣就使得上述工作變得非常簡(jiǎn)單。甚至不了解Web Service和其相關(guān)的標(biāo)準(zhǔn)、協(xié)議,也可以使用Visual Studio .NET編寫Web Service,并使用這個(gè)Web Service。下面就來(lái)用VB.NET實(shí)現(xiàn)一個(gè)Web Service,此Web Service和數(shù)據(jù)庫(kù)相關(guān),數(shù)據(jù)庫(kù)類型選用的是SqlServer。此Web Service提供了二個(gè)函數(shù)功能調(diào)用,其一名稱為Binding,用以實(shí)現(xiàn)數(shù)據(jù)綁定,其二名稱為Update,用以更新數(shù)據(jù)庫(kù)中的數(shù)據(jù)。
以下就是VB.NET實(shí)現(xiàn)Web Service的具體步驟:
1. 啟動(dòng)Visual Studio .NET。
2. 選擇菜單【文件】|【新建】|【項(xiàng)目】后,彈出【新建項(xiàng)目】對(duì)話框。
3. 將【項(xiàng)目類型】設(shè)置為【VB項(xiàng)目】。
4. 將【模板】設(shè)置為【ASP.NET Web 服務(wù)】。
5. 在【位置】的文本框中輸入"http://localhost/UpdateDataWebService"后,單擊【確定】按鈕,這樣在Visual Studio .NET就會(huì)計(jì)算機(jī)Internet信息服務(wù)的默認(rèn)目錄中創(chuàng)建一個(gè)名稱為"UpdateDataWebService"文件夾,里面存放的是此項(xiàng)目的文件。
6. 選中【解決方案資源管理器】中的"Service1.asmx"文件,單擊鼠標(biāo)右鍵,在彈出的菜單中選擇【查看代碼】,則進(jìn)入Service1.asmx.vb的編輯界面。
7. 在Service1.asmx..vb的首部,在導(dǎo)入命名空間的代碼區(qū)中添加下列代碼,下列代碼作用是導(dǎo)入命名空間System.Data.SqlClient:
- Imports System.Data.SqlClient
8. 在Service1.asmx..vb文件的"Public Class Service1 Inherits System.Web.Services.WebService"代碼后,添加下列代碼,下列代碼是在Web Service中定義二個(gè)功能調(diào)用:
- 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. 保存上述的修改,一個(gè)簡(jiǎn)單的操作Sql Server數(shù)據(jù)庫(kù)的Web Service就完成了,此時(shí)單擊快捷鍵F5,此Web Service就開始運(yùn)行,并可以對(duì)外提供服務(wù)了。
- 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 服務(wù)設(shè)計(jì)器生成的代碼 "
- Public Sub New ( )
- MyBase.New ( )
- '該調(diào)用是 Web 服務(wù)設(shè)計(jì)器所必需的。
- InitializeComponent ( )
- '在 InitializeComponent ( ) 調(diào)用之后添加您自己的初始化代碼
- End Sub
- 'Web 服務(wù)設(shè)計(jì)器所必需的
- Private components As System.ComponentModel.IContainer
- '注意:以下過(guò)程是 Web 服務(wù)設(shè)計(jì)器所必需的
- '可以使用 Web 服務(wù)設(shè)計(jì)器修改此過(guò)程。
- '不要使用代碼編輯器修改它。
- Private Sub InitializeComponent ( )
- components = New System.ComponentModel.Container ( )
- End Sub
- Protected Overloads Overrides Sub Dispose ( ByVal disposing As Boolean )
- 'CODEGEN: 此過(guò)程是 Web 服務(wù)設(shè)計(jì)器所必需的
- '不要使用代碼編輯器修改它。
- If disposing Then
- If Not ( components Is Nothing ) Then
- components.Dispose ( )
- End If
- End If
- MyBase.Dispose ( disposing )
- End Sub
- #End Region
- ' Web 服務(wù)示例
- ' HelloWorld ( ) 示例服務(wù)返回字符串 Hello World。
- ' 若要生成項(xiàng)目,請(qǐng)取消注釋以下行,然后保存并生成項(xiàng)目。
- ' 若要測(cè)試此 Web 服務(wù),請(qǐng)確保 .asmx 文件為起始頁(yè)
- ' 并按 F5 鍵。
- '
- ' Public Function HelloWorld ( ) As String
- ' HelloWorld = "Hello World"
- ' End Function
- End Class
以上介紹VB.NET實(shí)現(xiàn)Web Service
【編輯推薦】