这里介绍VB.NET调用Web Service,VB.NET就像是使用本地的类库一样使用Web Service中提供的各种功能。所以有些人说,Web Service从实质上说,就是通过HTTP调用远程组件的一种方式。

自己很喜欢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可参看下面进程中的第七步。

全面解说VB.NET调用Web Service(VB.NET教程)  VB.NET调用Web Service 第1张

在下面介绍的这个数据库应用程序是经过运用上面的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组件完结数据绑定:

  1. PrivateSubButton1_Click(ByValsenderAsSystem.Object,
    ByValeAsSystem.EventArgs)HandlesButton1.Click
  2. DimMyServiceAsNewlocalhost.Service1()
  3. DataGrid1.DataSource=MyService.Binding()
  4. DataGrid1.DataMember="Cust"
  5. EndSub

10. 用下列代码替换Form1.vb中的Button2的Click事情对应的处理代码,下列代码功用是运用Web Service中供给的"Update"服务完结经过DataGrid来修正数据库数据:

  1. PrivateSubButton2_Click(ByValsenderAsSystem.Object,
    ByValeAsSystem.EventArgs)HandlesButton2.Click
  2. DimMyServiceAsNewlocalhost.Service1()
  3. DimdsAsDataSet=DataGrid1.DataSource
  4. DimdsChangesAsDataSet=ds.GetChanges()
  5. IfNot(dsChangesIsNothing)Then
  6. ds.Merge(MyService.Update(dsChanges),True)
  7. EndIf
  8. EndSub

11. 至此, 【TestWebService】项目的悉数作业就完结了,VB.NET调用Web Service是不是很简单。此刻单击快捷键F5运转程序后。单击程序中的【绑定】按钮就会对程序中的DataGrid组件完结数据绑定,单击程序中的【修正】按钮,则程序会依据DataGrid中的内容来更新数据库。

12. Form1.vb的代码清单如下:

  1. PublicClassForm1
  2. InheritsSystem.Windows.Forms.Form
  3. #Region"Windows窗体规划器生成的代码"
  4. PublicSubNew()
  5. MyBase.New()
  6. '该调用是Windows窗体规划器所必需的。
  7. InitializeComponent()
  8. '在InitializeComponent()调用之后增加任何初始化
  9. EndSub
  10. '窗体重写处置以整理组件列表。
  11. ProtectedOverloadsOverridesSubDispose(ByValdisposingAsBoolean)
  12. IfdisposingThen
  13. IfNot(componentsIsNothing)Then
  14. components.Dispose()
  15. EndIf
  16. EndIf
  17. MyBase.Dispose(disposing)
  18. EndSub
  19. 'Windows窗体规划器所必需的
  20. PrivatecomponentsAsSystem.ComponentModel.IContainer
  21. '留意:以下进程是Windows窗体规划器所必需的
  22. '能够运用Windows窗体规划器修正此进程。
  23. '不要运用代码修正器修正它。
  24. FriendWithEventsButton1AsSystem.Windows.Forms.Button
  25. FriendWithEventsButton2AsSystem.Windows.Forms.Button
  26. FriendWithEventsDataGrid1AsSystem.Windows.Forms.DataGrid
  27. <System.Diagnostics.DebuggerStepThrough()>PrivateSubInitializeComponent()
  28. Me.Button1=NewSystem.Windows.Forms.Button()
  29. Me.Button2=NewSystem.Windows.Forms.Button()
  30. Me.DataGrid1=NewSystem.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=NewSystem.Drawing.Point(56,216)
  35. Me.Button1.Name="Button1"
  36. Me.Button1.Size=NewSystem.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=NewSystem.Drawing.Point(168,216)
  41. Me.Button2.Name="Button2"
  42. Me.Button2.Size=NewSystem.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=NewSystem.Drawing.Size(292,192)
  50. Me.DataGrid1.TabIndex=2
  51. Me.AutoScaleBaseSize=NewSystem.Drawing.Size(6,14)
  52. Me.ClientSize=NewSystem.Drawing.Size(292,273)
  53. Me.Controls.AddRange(NewSystem.Windows.Forms.Control(){Me.DataGrid1,Me.Button2,Me.Button1})
  54. Me.Name="Form1"
  55. Me.Text="测验WebService"
  56. CType(Me.DataGrid1,System.ComponentModel.ISupportInitialize).EndInit()
  57. Me.ResumeLayout(False)
  58. EndSub
  59. #EndRegion
  60. PrivateSubButton1_Click(ByValsenderAsSystem.Object,
    ByValeAsSystem.EventArgs)HandlesButton1.Click
  61. DimMyServiceAsNewlocalhost.Service1()
  62. DataGrid1.DataSource=MyService.Binding()
  63. DataGrid1.DataMember="Cust"
  64. EndSub
  65. PrivateSubButton2_Click(ByValsenderAsSystem.Object,
    ByValeAsSystem.EventArgs)HandlesButton2.Click
  66. DimMyServiceAsNewlocalhost.Service1()
  67. DimdsAsDataSet=DataGrid1.DataSource
  68. DimdsChangesAsDataSet=ds.GetChanges()
  69. IfNot(dsChangesIsNothing)Then
  70. ds.Merge(MyService.Update(dsChanges),True)
  71. EndIf
  72. EndSub
  73. EndClass

【修正引荐】

  1. 代码叙述VB.NET完结数据绑定
  2. VB.NET TextBox组件高手经验谈
  3. 瞬间把握VB.NET Web Service
  4. 实例剖析VB.NET Treeview结构
  5. 百宝箱之VB.NET规划制造窗体
转载请说明出处
知优网 » 全面解说VB.NET调用Web Service(VB.NET教程)

发表评论

您需要后才能发表评论