自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

全面講解VB.NET調(diào)用Web Service

開發(fā) 后端
這里介紹VB.NET調(diào)用Web Service,VB.NET就像是使用本地的類庫一樣使用Web Service中提供的各種功能。所以有些人說,Web Service從實(shí)質(zhì)上說,就是通過HTTP調(diào)用遠(yuǎn)程組件的一種方式。

本人很喜歡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ù)綁定:

  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ù)實(shí)現(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】項(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的代碼清單如下:

  1. Public Class Form1  
  2. Inherits System.Windows.Forms.Form  
  3. #Region " Windows 窗體設(shè)計(jì)器生成的代碼 "  
  4. Public Sub New ( )  
  5. MyBase.New ( )  
  6. '該調(diào)用是 Windows 窗體設(shè)計(jì)器所必需的。  
  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è)計(jì)器所必需的  
  20. Private components As System.ComponentModel.IContainer  
  21. '注意:以下過程是 Windows 窗體設(shè)計(jì)器所必需的  
  22. '可以使用 Windows 窗體設(shè)計(jì)器修改此過程。  
  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 

【編輯推薦】

  1. 代碼講述VB.NET實(shí)現(xiàn)數(shù)據(jù)綁定
  2. VB.NET TextBox組件高手經(jīng)驗(yàn)談
  3. 瞬間掌握VB.NET Web Service
  4. 實(shí)例分析VB.NET Treeview結(jié)構(gòu)
  5. 百寶箱之VB.NET設(shè)計(jì)制作窗體
責(zé)任編輯:佚名 來源: 新浪博客
相關(guān)推薦

2009-10-13 11:22:46

VB.NET調(diào)用Web

2009-10-15 11:42:05

VB.Net賦值語句

2009-11-02 15:57:36

VB.NET WEB

2009-10-13 10:21:58

VB.NET實(shí)現(xiàn)Web

2009-10-29 09:06:26

VB.NET Web

2009-10-13 09:33:49

VB.NET Web

2009-11-10 16:20:25

VB.NET全局熱鍵

2010-01-13 10:46:42

VB.NET Dock

2009-10-15 17:50:48

VB.NET調(diào)用API

2009-10-27 12:20:06

VB.NET多線程應(yīng)用

2009-10-28 10:04:53

VB.NET XmlW

2009-10-28 17:44:31

VB.NET語言

2010-01-11 16:04:10

VB.NET使用wit

2009-10-20 10:16:24

VB.NET COMB

2009-11-04 10:54:53

VB.NET MOVE

2009-10-15 10:57:16

VB.NET Text

2009-10-29 15:16:02

VB.NET文件傳送

2009-10-14 15:20:21

VB.NET窗體指針

2009-11-02 14:48:45

VB.NET HOOK

2009-11-10 16:46:52

VB.NET指針應(yīng)用
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)