日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
全面講解VB.NET調(diào)用WebService

本人很喜歡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)處于對(duì)外提供服務(wù)狀態(tài),VB.NET就可以通過HTTP"調(diào)用"來使用這些服務(wù)了。當(dāng)然前提是要了解Web Service對(duì)外提供服務(wù)所對(duì)應(yīng)的URL,當(dāng)了解到Web Service對(duì)應(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ù),對(duì)程序中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)目】對(duì)話框。

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引用】對(duì)話框中的【地址】文本框中輸入"http://localhost/UpdateDataWebService/Service1.asmx "后,單擊回車鍵后,則在【TestWebService】項(xiàng)目中加入了Web引用。請(qǐng)注意"http://localhost/UpdateDataWebService/Service1.asmx "就是上面完成的Web Service對(duì)外提供服務(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事件對(duì)應(yīng)的處理代碼。

9. 把VB.NET的當(dāng)前窗口切換到Form1.vb的代碼編輯窗口,并用下列代碼替換Form1.vb中的Button1的Click事件對(duì)應(yīng)的處理代碼,下列代碼功能是VB.NET調(diào)用Web Service中提供的"Binding"服務(wù)對(duì)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事件對(duì)應(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是不是很簡(jiǎn)單。此時(shí)單擊快捷鍵F5運(yùn)行程序后。單擊程序中的【綁定】按鈕就會(huì)對(duì)程序中的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.  ( ) > 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 = "測(cè)試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

網(wǎng)站欄目:全面講解VB.NET調(diào)用WebService
標(biāo)題URL:http://m.5511xx.com/article/dpshcph.html