|
|

VB.Net实现Web Service的基础
![]()
图04【TestWebService】项目中组件排列位置和顺序
10. 把Visual Studio .Net的当前窗口切换到Form1.vb的代码编辑窗口,并用下列代码替换Form1.vb中的Button1的Click事件对应的处理代码,下列代码功能是使用Web Service中提供的"Binding"服务对DataGrid组件实现数据绑定:
以下为引用的内容:
Private Sub Button1_Click ( ByVal sender As System.Object , ByVal e As System.EventArgs ) Handles Button1.Click Dim MyService As New localhost.Service1 ( ) DataGrid1.DataSource = MyService.Binding ( ) DataGrid1.DataMember = "Cust"End Sub
11. 用下列代码替换Form1.vb中的Button2的Click事件对应的处理代码,下列代码功能是使用Web Service中提供的"Update"服务实现通过DataGrid来修改数据库数据:
以下为引用的内容:
Private Sub Button2_Click (ByVal sender As System.Object , ByVal e As System.EventArgs ) Handles Button2.Click Dim MyService As New localhost.Service1 ( ) Dim ds As DataSet = DataGrid1.DataSource Dim dsChanges As DataSet = ds.GetChanges ( ) If Not ( dsChanges Is Nothing ) Thends.Merge ( MyService.Update ( dsChanges ) , True ) End IfEnd Sub
12. 至此, 【TestWebService】项目的全部工作就完成了,调用Web Service是不是很简单。此时单击快捷键F5运行程序后。单击程序中的【绑定】按钮就会对程序中的DataGrid组件实现数据绑定,单击程序中的【修改】按钮,则程序会根据DataGrid中的内容来更新数据库,图05就是【TestWebService】的运行界面:
![]()
图05:【TestWebService】的运行界面
13. Form1.vb的代码清单如下:
以下为引用的内容:
Public Class Form1Inherits System.Windows.Forms.Form#Region " Windows 窗体设计器生成的代码 "Public Sub New ( ) MyBase.New ( ) '该调用是 Windows 窗体设计器所必需的。 InitializeComponent ( ) '在 InitializeComponent ( ) 调用之后添加任何初始化End Sub'窗体重写处置以清理组件列表。Protected Overloads Overrides Sub Dispose ( ByVal disposing As Boolean ) If disposing ThenIf Not ( components Is Nothing ) Then components.Dispose ( ) End If End If MyBase.Dispose ( disposing ) End Sub'Windows 窗体设计器所必需的Private components As System.ComponentModel.IContainer '注意:以下过程是 Windows 窗体设计器所必需的 '可以使用 Windows 窗体设计器修改此过程。 '不要使用代码编辑器修改它。 Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents Button2 As System.Windows.Forms.Button Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid Private Sub InitializeComponent ( ) Me.Button1 = New System.Windows.Forms.Button ( ) Me.Button2 = New System.Windows.Forms.Button ( ) Me.DataGrid1 = New System.Windows.Forms.DataGrid ( ) CType ( Me.DataGrid1 , System.ComponentModel.ISupportInitialize ) .BeginInit ( ) Me.SuspendLayout ( ) Me.Button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat Me.Button1.Location = New System.Drawing.Point ( 56 , 216 ) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size ( 75 , 32 ) Me.Button1.TabIndex = 0 Me.Button1.Text = "绑定" Me.Button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat Me.Button2.Location = New System.Drawing.Point ( 168 , 216 ) Me.Button2.Name = "Button2" Me.Button2.Size = New System.Drawing.Size ( 75 , 32 ) Me.Button2.TabIndex = 1 Me.Button2.Text = "修改" Me.DataGrid1.DataMember = "" Me.DataGrid1.Dock = System.Windows.Forms.DockStyle.Top Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText Me.DataGrid1.Name = "DataGrid1" Me.DataGrid1.Size = New System.Drawing.Size ( 292 , 192 ) Me.DataGrid1.TabIndex = 2 Me.AutoScaleBaseSize = New System.Drawing.Size ( 6 , 14 ) Me.ClientSize = New System.Drawing.Size ( 292 , 273 ) Me.Controls.AddRange ( New System.Windows.Forms.Control ( ) {Me.DataGrid1 , Me.Button2 , Me.Button1} ) Me.Name = "Form1" Me.Text = "测试Web Service" CType ( Me.DataGrid1 ,System.ComponentModel.ISupportInitialize ) .EndInit ( ) Me.ResumeLayout ( False ) End Sub#End RegionPrivate Sub Button1_Click ( ByVal sender As System.Object , ByVal e As System.EventArgs ) Handles Button1.Click Dim MyService As New localhost.Service1 ( ) DataGrid1.DataSource = MyService.Binding ( ) DataGrid1.DataMember = "Cust"End SubPrivate Sub Button2_Click ( ByVal sender As System.Object , ByVal e As System.EventArgs ) Handles Button2.Click Dim MyService As New localhost.Service1 ( ) Dim ds As DataSet = DataGrid1.DataSource Dim dsChanges As DataSet = ds.GetChanges ( ) If Not ( dsChanges Is Nothing ) Thends.Merge ( MyService.Update ( dsChanges ) , True ) End IfEnd SubEnd Class
六.总结
本文介绍了Web Service在目前大行其道的原因,并结合二个示例介绍Visual Basic .Net在Web Service的实现和调用上的强大功能,正如本文前面所说,Visual Studio .Net的出现使得web Service的这项原先繁杂的工作变得异常的简单,因为Visual Basic .Net已经替代我们作了这些描述性的、基础的底层工作,以至于你不需要了解为什么,只需要知道你要实现什么就可以编写、调用Web Service了。 在实现Web Service在数据库方面应用所显示 |
|