全面讲解VB.NET调用Web Service

开发 后端
这里介绍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可参阅下面步骤中的第七步。

在下面介绍的这个数据库应用程序是通过使用上面的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. 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事件对应的处理代码,下列代码功能是使用Web Service中提供的"Update"服务实现通过DataGrid来修改数据库数据:

  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调用Web Service是不是很简单。此时单击快捷键F5运行程序后。单击程序中的【绑定】按钮就会对程序中的DataGrid组件实现数据绑定,单击程序中的【修改】按钮,则程序会根据DataGrid中的内容来更新数据库。

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

  1. Public Class Form1  
  2. Inherits System.Windows.Forms.Form  
  3. #Region " Windows 窗体设计器生成的代码 "  
  4. Public Sub New ( )  
  5. MyBase.New ( )  
  6. '该调用是 Windows 窗体设计器所必需的。  
  7. InitializeComponent ( )  
  8. '在 InitializeComponent ( ) 调用之后添加任何初始化  
  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 窗体设计器所必需的  
  20. Private components As System.ComponentModel.IContainer  
  21. '注意:以下过程是 Windows 窗体设计器所必需的  
  22. '可以使用 Windows 窗体设计器修改此过程。  
  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实现数据绑定
  2. VB.NET TextBox组件高手经验谈
  3. 瞬间掌握VB.NET Web Service
  4. 实例分析VB.NET Treeview结构
  5. 百宝箱之VB.NET设计制作窗体
责任编辑:佚名 来源: 新浪博客
相关推荐

2009-10-13 11:22:46

VB.NET调用Web

2009-10-15 11:42:05

VB.Net赋值语句

2009-10-29 09:06:26

VB.NET Web

2009-10-13 10:21:58

VB.NET实现Web

2009-11-02 15:57:36

VB.NET WEB

2009-11-10 16:20:25

VB.NET全局热键

2009-10-13 09:33:49

VB.NET Web

2009-10-15 17:50:48

VB.NET调用API

2010-01-13 10:46:42

VB.NET Dock

2009-10-27 12:20:06

VB.NET多线程应用

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-10-28 10:04:53

VB.NET XmlW

2009-11-10 16:46:52

VB.NET指针应用

2009-11-02 14:48:45

VB.NET HOOK

2009-10-12 13:54:22

VB.NET Data

2009-10-23 13:10:14

VB.NET List

2009-10-29 15:16:02

VB.NET文件传送

2009-10-14 15:20:21

VB.NET窗体指针
点赞
收藏

51CTO技术栈公众号