千家信息网

VB.NET如何调用Web Service

发表于:2024-09-24 作者:千家信息网编辑
千家信息网最后更新 2024年09月24日,这篇文章将为大家详细讲解有关VB.NET如何调用Web Service,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。VB.NET调用Web Service提供服务来
千家信息网最后更新 2024年09月24日VB.NET如何调用Web Service

这篇文章将为大家详细讲解有关VB.NET如何调用Web Service,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

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

关于"VB.NET如何调用Web Service"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

0