溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

VB.NET如何調(diào)用Web Service

發(fā)布時間:2021-12-01 15:44:08 來源:億速云 閱讀:718 作者:小新 欄目:編程語言

這篇文章將為大家詳細(xì)講解有關(guān)VB.NET如何調(diào)用Web Service,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

VB.NET調(diào)用Web Service提供服務(wù)來編寫數(shù)據(jù)庫應(yīng)用程序的具體步驟,:

1. 啟動Visual Studio .Net。

2. 選擇菜單【文件】|【新建】|【項目】后,彈出【新建項目】對話框。

3. 將【項目類型】設(shè)置為【Visual Basic項目】。

4. 將【模板】設(shè)置為【W(wǎng)indows應(yīng)用程序】。

5. 在【名稱】文本框中輸入【TestWebService】。

6. 在【位置】的文本框中輸入【E:\VS.NET項目】,然后單擊【確定】按鈕,這樣在"E:\VS.NET項目"中就產(chǎn)生了名稱為"TestWebService"文件夾,里面存放的就是TestWebService項目的所有文件。

7. 選擇【解決方案資源管理器】|【引用】后,單擊鼠標(biāo)右鍵,在彈出的菜單中選擇【添加Web 引用】,在彈出的【添加Web引用】對話框中的【地址】文本框中輸入"http://localhost/UpdateDataWebService/Service1.asmx "后,單擊回車鍵后,則在【TestWebService】項目中加入了Web引用。請注意"http://localhost/UpdateDataWebService/Service1.asmx "就是上面完成的Web Service對外提供服務(wù)的URL地址。

8. 從【工具箱】中的【W(wǎng)indows窗體組件】選項卡中往Form1窗體中拖入下列組件,并執(zhí)行相應(yīng)的操作:

一個DataGrid組件。

二個Button組件,分別是Button1至Button2,并在這二個Button組件拖入Form1的設(shè)計窗體后,分別雙擊它們,則系統(tǒng)會在Form1.vb文件分別產(chǎn)生這二個組件的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組件實現(xiàn)數(shù)據(jù)綁定:

  1. Private Sub Button1_Click ( ByVal sender As System.Object , 
    ByVal e As System.EventArgs ) Handles Button1.Click  

  2. Dim MyService As New localhost.Service1 ( )  

  3. DataGrid1.DataSource = MyService.Binding ( )  

  4. DataGrid1.DataMember = "Cust" 

  5. End Sub 

10. 用下列代碼替換Form1.vb中的Button2的Click事件對應(yīng)的處理代碼,下列代碼功能是使用Web Service中提供的"Update"服務(wù)實現(xiàn)通過DataGrid來修改數(shù)據(jù)庫數(shù)據(jù):

  1. Private Sub Button2_Click ( ByVal sender As System.Object, 
    ByVal e As System.EventArgs ) Handles Button2.Click  

  2. Dim MyService As New localhost.Service1 ( )  

  3. Dim ds As DataSet = DataGrid1.DataSource  

  4. Dim dsChanges As DataSet = ds.GetChanges ( )  

  5. If Not ( dsChanges Is Nothing ) Then  

  6. ds.Merge ( MyService.Update ( dsChanges ) , True )  

  7. End If  

  8. End Sub 

11. 至此, 【TestWebService】項目的全部工作就完成了,VB.NET調(diào)用Web Service是不是很簡單。此時單擊快捷鍵F5運(yùn)行程序后。單擊程序中的【綁定】按鈕就會對程序中的DataGrid組件實現(xiàn)數(shù)據(jù)綁定,單擊程序中的【修改】按鈕,則程序會根據(jù)DataGrid中的內(nèi)容來更新數(shù)據(jù)庫。

12. Form1.vb的代碼清單如下:

  1. Public Class Form1  

  2. Inherits System.Windows.Forms.Form  

  3. #Region " Windows 窗體設(shè)計器生成的代碼 "  

  4. Public Sub New ( )  

  5. MyBase.New ( )  

  6. '該調(diào)用是 Windows 窗體設(shè)計器所必需的。  

  7. InitializeComponent ( )  

  8. '在 InitializeComponent ( ) 調(diào)用之后添加任何初始化  

  9. End Sub  

  10. '窗體重寫處置以清理組件列表。  

  11. Protected Overloads Overrides Sub Dispose ( ByVal disposing As Boolean )  

  12. If disposing Then  

  13. If Not ( components Is Nothing ) Then  

  14. components.Dispose ( )  

  15. End If  

  16. End If  

  17. MyBase.Dispose ( disposing )  

  18. End Sub  

  19. 'Windows 窗體設(shè)計器所必需的  

  20. Private components As System.ComponentModel.IContainer  

  21. '注意:以下過程是 Windows 窗體設(shè)計器所必需的  

  22. '可以使用 Windows 窗體設(shè)計器修改此過程。  

  23. '不要使用代碼編輯器修改它。  

  24. Friend WithEvents Button1 As System.Windows.Forms.Button  

  25. Friend WithEvents Button2 As System.Windows.Forms.Button  

  26. Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid  

  27. <System.Diagnostics.DebuggerStepThrough ( ) > Private Sub InitializeComponent ( )  

  28. Me.Button1 = New System.Windows.Forms.Button ( )  

  29. Me.Button2 = New System.Windows.Forms.Button ( )  

  30. Me.DataGrid1 = New System.Windows.Forms.DataGrid ( )  

  31. CType ( Me.DataGrid1 , System.ComponentModel.ISupportInitialize ) .BeginInit ( )  

  32. Me.SuspendLayout ( )  

  33. Me.Button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat  

  34. Me.Button1.Location = New System.Drawing.Point ( 56 , 216 )  

  35. Me.Button1.Name = "Button1" 

  36. Me.Button1.Size = New System.Drawing.Size ( 75 , 32 )  

  37. Me.Button1.TabIndex = 0 

  38. Me.Button1.Text = "綁定" 

  39. Me.Button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat  

  40. Me.Button2.Location = New System.Drawing.Point ( 168 , 216 )  

  41. Me.Button2.Name = "Button2" 

  42. Me.Button2.Size = New System.Drawing.Size ( 75 , 32 )  

  43. Me.Button2.TabIndex = 1 

  44. Me.Button2.Text = "修改" 

  45. Me.DataGrid1.DataMember = "" 

  46. Me.DataGrid1.Dock = System.Windows.Forms.DockStyle.Top  

  47. Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText  

  48. Me.DataGrid1.Name = "DataGrid1" 

  49. Me.DataGrid1.Size = New System.Drawing.Size ( 292 , 192 )  

  50. Me.DataGrid1.TabIndex = 2 

  51. Me.AutoScaleBaseSize = New System.Drawing.Size ( 6 , 14 )  

  52. Me.ClientSize = New System.Drawing.Size ( 292 , 273 )  

  53. Me.Controls.AddRange ( New System.Windows.Forms.Control ( ) {Me.DataGrid1 , Me.Button2 , Me.Button1} )  

  54. Me.Name = "Form1" 

  55. Me.Text = "測試Web Service" 

  56. CType ( Me.DataGrid1 , System.ComponentModel.ISupportInitialize ) .EndInit ( )  

  57. Me.ResumeLayout ( False )  

  58. End Sub  

  59. #End Region  

  60. Private Sub Button1_Click ( ByVal sender As System.Object , 
    ByVal e As System.EventArgs ) Handles Button1.Click  

  61. Dim MyService As New localhost.Service1 ( )  

  62. DataGrid1.DataSource = MyService.Binding ( )  

  63. DataGrid1.DataMember = "Cust" 

  64. End Sub  

  65. Private Sub Button2_Click ( ByVal sender As System.Object , 
    ByVal e As System.EventArgs ) Handles Button2.Click  

  66. Dim MyService As New localhost.Service1 ( )  

  67. Dim ds As DataSet = DataGrid1.DataSource  

  68. Dim dsChanges As DataSet = ds.GetChanges ( )  

  69. If Not ( dsChanges Is Nothing ) Then  

  70. ds.Merge ( MyService.Update ( dsChanges ) , True )  

  71. End If  

  72. End Sub  

  73. End Class 

關(guān)于“VB.NET如何調(diào)用Web Service”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI