全面講解VB.NET調(diào)用Web Service
本人很喜歡VB.NET,在工作中也很喜歡總結(jié)關(guān)于VB.NET調(diào)用Web Service的經(jīng)驗(yàn)教訓(xùn),下面就這個(gè)問題來詳細(xì)說說吧。當(dāng)Web Service已經(jīng)處于對外提供服務(wù)狀態(tài),VB.NET就可以通過HTTP"調(diào)用"來使用這些服務(wù)了。當(dāng)然前提是要了解Web Service對外提供服務(wù)所對應(yīng)的URL,當(dāng)了解到Web Service對應(yīng)的URL后,VB.NET就像是使用本地的類庫一樣使用Web Service中提供的各種功能。所以有些人說,Web Service從實(shí)質(zhì)上說,就是通過HTTP調(diào)用遠(yuǎn)程組件的一種方式。在VB.NET具體實(shí)現(xiàn)加入Web Service可參閱下面步驟中的第七步。
在下面介紹的這個(gè)數(shù)據(jù)庫應(yīng)用程序是通過使用上面的Web Service中提供的"Binding"服務(wù),對程序中DataGrid組件實(shí)現(xiàn)數(shù)據(jù)綁定,提供使用Web Service中提供的"Update"服務(wù),通過程序中的DataGrid來修改數(shù)據(jù)庫。下面就是VB.NET調(diào)用Web Service提供服務(wù)來編寫數(shù)據(jù)庫應(yīng)用程序的具體步驟,:
1. 啟動(dòng)Visual Studio .Net。
2. 選擇菜單【文件】|【新建】|【項(xiàng)目】后,彈出【新建項(xiàng)目】對話框。
3. 將【項(xiàng)目類型】設(shè)置為【Visual Basic項(xiàng)目】。
4. 將【模板】設(shè)置為【W(wǎng)indows應(yīng)用程序】。
5. 在【名稱】文本框中輸入【TestWebService】。
6. 在【位置】的文本框中輸入【E:\VS.NET項(xiàng)目】,然后單擊【確定】按鈕,這樣在"E:\VS.NET項(xiàng)目"中就產(chǎn)生了名稱為"TestWebService"文件夾,里面存放的就是TestWebService項(xiàng)目的所有文件。
7. 選擇【解決方案資源管理器】|【引用】后,單擊鼠標(biāo)右鍵,在彈出的菜單中選擇【添加Web 引用】,在彈出的【添加Web引用】對話框中的【地址】文本框中輸入"http://localhost/UpdateDataWebService/Service1.asmx "后,單擊回車鍵后,則在【TestWebService】項(xiàng)目中加入了Web引用。請注意"http://localhost/UpdateDataWebService/Service1.asmx "就是上面完成的Web Service對外提供服務(wù)的URL地址。
8. 從【工具箱】中的【W(wǎng)indows窗體組件】選項(xiàng)卡中往Form1窗體中拖入下列組件,并執(zhí)行相應(yīng)的操作:
一個(gè)DataGrid組件。
二個(gè)Button組件,分別是Button1至Button2,并在這二個(gè)Button組件拖入Form1的設(shè)計(jì)窗體后,分別雙擊它們,則系統(tǒng)會(huì)在Form1.vb文件分別產(chǎn)生這二個(gè)組件的Click事件對應(yīng)的處理代碼。
9. 把VB.NET的當(dāng)前窗口切換到Form1.vb的代碼編輯窗口,并用下列代碼替換Form1.vb中的Button1的Click事件對應(yīng)的處理代碼,下列代碼功能是VB.NET調(diào)用Web Service中提供的"Binding"服務(wù)對DataGrid組件實(shí)現(xiàn)數(shù)據(jù)綁定:
- 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
10. 用下列代碼替換Form1.vb中的Button2的Click事件對應(yīng)的處理代碼,下列代碼功能是使用Web Service中提供的"Update"服務(wù)實(shí)現(xiàn)通過DataGrid來修改數(shù)據(jù)庫數(shù)據(jù):
- 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 ) Then
- ds.Merge ( MyService.Update ( dsChanges ) , True )
- End If
- End Sub
11. 至此, 【TestWebService】項(xiàng)目的全部工作就完成了,VB.NET調(diào)用Web Service是不是很簡單。此時(shí)單擊快捷鍵F5運(yùn)行程序后。單擊程序中的【綁定】按鈕就會(huì)對程序中的DataGrid組件實(shí)現(xiàn)數(shù)據(jù)綁定,單擊程序中的【修改】按鈕,則程序會(huì)根據(jù)DataGrid中的內(nèi)容來更新數(shù)據(jù)庫。
12. Form1.vb的代碼清單如下:
- Public Class Form1
- Inherits System.Windows.Forms.Form
- #Region " Windows 窗體設(shè)計(jì)器生成的代碼 "
- Public Sub New ( )
- MyBase.New ( )
- '該調(diào)用是 Windows 窗體設(shè)計(jì)器所必需的。
- InitializeComponent ( )
- '在 InitializeComponent ( ) 調(diào)用之后添加任何初始化
- End Sub
- '窗體重寫處置以清理組件列表。
- Protected Overloads Overrides Sub Dispose ( ByVal disposing As Boolean )
- If disposing Then
- If Not ( components Is Nothing ) Then
- components.Dispose ( )
- End If
- End If
- MyBase.Dispose ( disposing )
- End Sub
- 'Windows 窗體設(shè)計(jì)器所必需的
- Private components As System.ComponentModel.IContainer
- '注意:以下過程是 Windows 窗體設(shè)計(jì)器所必需的
- '可以使用 Windows 窗體設(shè)計(jì)器修改此過程。
- '不要使用代碼編輯器修改它。
- Friend WithEvents Button1 As System.Windows.Forms.Button
- Friend WithEvents Button2 As System.Windows.Forms.Button
- Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
- <System.Diagnostics.DebuggerStepThrough ( ) > Private Sub InitializeComponent ( )
- Me.Button1 = New System.Windows.Forms.Button ( )
- Me.Button2 = New System.Windows.Forms.Button ( )
- Me.DataGrid1 = New System.Windows.Forms.DataGrid ( )
- CType ( Me.DataGrid1 , System.ComponentModel.ISupportInitialize ) .BeginInit ( )
- Me.SuspendLayout ( )
- Me.Button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat
- Me.Button1.Location = New System.Drawing.Point ( 56 , 216 )
- Me.Button1.Name = "Button1"
- Me.Button1.Size = New System.Drawing.Size ( 75 , 32 )
- Me.Button1.TabIndex = 0
- Me.Button1.Text = "綁定"
- Me.Button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat
- Me.Button2.Location = New System.Drawing.Point ( 168 , 216 )
- Me.Button2.Name = "Button2"
- Me.Button2.Size = New System.Drawing.Size ( 75 , 32 )
- Me.Button2.TabIndex = 1
- Me.Button2.Text = "修改"
- Me.DataGrid1.DataMember = ""
- Me.DataGrid1.Dock = System.Windows.Forms.DockStyle.Top
- Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
- Me.DataGrid1.Name = "DataGrid1"
- Me.DataGrid1.Size = New System.Drawing.Size ( 292 , 192 )
- Me.DataGrid1.TabIndex = 2
- Me.AutoScaleBaseSize = New System.Drawing.Size ( 6 , 14 )
- Me.ClientSize = New System.Drawing.Size ( 292 , 273 )
- Me.Controls.AddRange ( New System.Windows.Forms.Control ( ) {Me.DataGrid1 , Me.Button2 , Me.Button1} )
- Me.Name = "Form1"
- Me.Text = "測試Web Service"
- CType ( Me.DataGrid1 , System.ComponentModel.ISupportInitialize ) .EndInit ( )
- Me.ResumeLayout ( False )
- End Sub
- #End Region
- 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 ) Then
- ds.Merge ( MyService.Update ( dsChanges ) , True )
- End If
- End Sub
- End Class
【編輯推薦】