全面講解VB.NET調用Web Service
本人很喜歡VB.NET,在工作中也很喜歡總結關于VB.NET調用Web Service的經驗教訓,下面就這個問題來詳細說說吧。當Web Service已經處于對外提供服務狀態,VB.NET就可以通過HTTP"調用"來使用這些服務了。當然前提是要了解Web Service對外提供服務所對應的URL,當了解到Web Service對應的URL后,VB.NET就像是使用本地的類庫一樣使用Web Service中提供的各種功能。所以有些人說,Web Service從實質上說,就是通過HTTP調用遠程組件的一種方式。在VB.NET具體實現加入Web Service可參閱下面步驟中的第七步。
在下面介紹的這個數據庫應用程序是通過使用上面的Web Service中提供的"Binding"服務,對程序中DataGrid組件實現數據綁定,提供使用Web Service中提供的"Update"服務,通過程序中的DataGrid來修改數據庫。下面就是VB.NET調用Web Service提供服務來編寫數據庫應用程序的具體步驟,:
1. 啟動Visual Studio .Net。
2. 選擇菜單【文件】|【新建】|【項目】后,彈出【新建項目】對話框。
3. 將【項目類型】設置為【Visual Basic項目】。
4. 將【模板】設置為【Windows應用程序】。
5. 在【名稱】文本框中輸入【TestWebService】。
6. 在【位置】的文本框中輸入【E:\VS.NET項目】,然后單擊【確定】按鈕,這樣在"E:\VS.NET項目"中就產生了名稱為"TestWebService"文件夾,里面存放的就是TestWebService項目的所有文件。
7. 選擇【解決方案資源管理器】|【引用】后,單擊鼠標右鍵,在彈出的菜單中選擇【添加Web 引用】,在彈出的【添加Web引用】對話框中的【地址】文本框中輸入"http://localhost/UpdateDataWebService/Service1.asmx "后,單擊回車鍵后,則在【TestWebService】項目中加入了Web引用。請注意"http://localhost/UpdateDataWebService/Service1.asmx "就是上面完成的Web Service對外提供服務的URL地址。
8. 從【工具箱】中的【Windows窗體組件】選項卡中往Form1窗體中拖入下列組件,并執行相應的操作:
一個DataGrid組件。
二個Button組件,分別是Button1至Button2,并在這二個Button組件拖入Form1的設計窗體后,分別雙擊它們,則系統會在Form1.vb文件分別產生這二個組件的Click事件對應的處理代碼。
9. 把VB.NET的當前窗口切換到Form1.vb的代碼編輯窗口,并用下列代碼替換Form1.vb中的Button1的Click事件對應的處理代碼,下列代碼功能是VB.NET調用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
10. 用下列代碼替換Form1.vb中的Button2的Click事件對應的處理代碼,下列代碼功能是使用Web Service中提供的"Update"服務實現通過DataGrid來修改數據庫數據:
- 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】項目的全部工作就完成了,VB.NET調用Web Service是不是很簡單。此時單擊快捷鍵F5運行程序后。單擊程序中的【綁定】按鈕就會對程序中的DataGrid組件實現數據綁定,單擊程序中的【修改】按鈕,則程序會根據DataGrid中的內容來更新數據庫。
12. Form1.vb的代碼清單如下:
- Public Class Form1
- Inherits System.Windows.Forms.Form
- #Region " Windows 窗體設計器生成的代碼 "
- Public Sub New ( )
- MyBase.New ( )
- '該調用是 Windows 窗體設計器所必需的。
- InitializeComponent ( )
- '在 InitializeComponent ( ) 調用之后添加任何初始化
- 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 窗體設計器所必需的
- Private components As System.ComponentModel.IContainer
- '注意:以下過程是 Windows 窗體設計器所必需的
- '可以使用 Windows 窗體設計器修改此過程。
- '不要使用代碼編輯器修改它。
- 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
【編輯推薦】