這篇文章將為大家詳細(xì)講解有關(guān)VB.NET如何調(diào)用Web Service,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供天門網(wǎng)站建設(shè)、天門做網(wǎng)站、天門網(wǎng)站設(shè)計、天門網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、天門企業(yè)網(wǎng)站模板建站服務(wù),十載天門做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。
VB.NET調(diào)用Web Service提供服務(wù)來編寫數(shù)據(jù)庫應(yīng)用程序的具體步驟,:
1. 啟動Visual Studio .Net。
2. 選擇菜單【文件】|【新建】|【項目】后,彈出【新建項目】對話框。
3. 將【項目類型】設(shè)置為【Visual Basic項目】。
4. 將【模板】設(shè)置為【W(wǎng)indows應(yīng)用程序】。
5. 在【名稱】文本框中輸入【TestWebService】。
6. 在【位置】的文本框中輸入【E:\VS.NET項目】,然后單擊【確定】按鈕,這樣在"E:\VS.NET項目"中就產(chǎn)生了名稱為"TestWebService"文件夾,里面存放的就是TestWebService項目的所有文件。
7. 選擇【解決方案資源管理器】|【引用】后,單擊鼠標(biāo)右鍵,在彈出的菜單中選擇【添加Web 引用】,在彈出的【添加Web引用】對話框中的【地址】文本框中輸入"http://localhost/UpdateDataWebService/Service1.asmx "后,單擊回車鍵后,則在【TestWebService】項目中加入了Web引用。請注意"http://localhost/UpdateDataWebService/Service1.asmx "就是上面完成的Web Service對外提供服務(wù)的URL地址。
8. 從【工具箱】中的【W(wǎng)indows窗體組件】選項卡中往Form1窗體中拖入下列組件,并執(zhí)行相應(yīng)的操作:
一個DataGrid組件。
二個Button組件,分別是Button1至Button2,并在這二個Button組件拖入Form1的設(shè)計窗體后,分別雙擊它們,則系統(tǒng)會在Form1.vb文件分別產(chǎn)生這二個組件的Click事件對應(yīng)的處理代碼。
9. 把VB.NET的當(dāng)前窗口切換到Form1.vb的代碼編輯窗口,并用下列代碼替換Form1.vb中的Button1的Click事件對應(yīng)的處理代碼,下列代碼功能是VB.NET調(diào)用Web Service中提供的"Binding"服務(wù)對DataGrid組件實現(xiàn)數(shù)據(jù)綁定:
Private Sub Button1_Click ( ByVal sender As System.Object ,
ByVal e As System.EventArgs ) Handles Button1.ClickDim MyService As New localhost.Service1 ( )
DataGrid1.DataSource = MyService.Binding ( )
DataGrid1.DataMember = "Cust"
End Sub
10. 用下列代碼替換Form1.vb中的Button2的Click事件對應(yīng)的處理代碼,下列代碼功能是使用Web Service中提供的"Update"服務(wù)實現(xiàn)通過DataGrid來修改數(shù)據(jù)庫數(shù)據(jù):
Private Sub Button2_Click ( ByVal sender As System.Object,
ByVal e As System.EventArgs ) Handles Button2.ClickDim MyService As New localhost.Service1 ( )
Dim ds As DataSet = DataGrid1.DataSource
Dim dsChanges As DataSet = ds.GetChanges ( )
If Not ( dsChanges Is Nothing ) Then
ds.Merge ( MyService.Update ( dsChanges ) , True )
End If
End Sub
11. 至此, 【TestWebService】項目的全部工作就完成了,VB.NET調(diào)用Web Service是不是很簡單。此時單擊快捷鍵F5運行程序后。單擊程序中的【綁定】按鈕就會對程序中的DataGrid組件實現(xiàn)數(shù)據(jù)綁定,單擊程序中的【修改】按鈕,則程序會根據(jù)DataGrid中的內(nèi)容來更新數(shù)據(jù)庫。
12. Form1.vb的代碼清單如下:
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows 窗體設(shè)計器生成的代碼 "
Public Sub New ( )
MyBase.New ( )
'該調(diào)用是 Windows 窗體設(shè)計器所必需的。
InitializeComponent ( )
'在 InitializeComponent ( ) 調(diào)用之后添加任何初始化
End Sub
'窗體重寫處置以清理組件列表。
Protected Overloads Overrides Sub Dispose ( ByVal disposing As Boolean )
If disposing Then
If Not ( components Is Nothing ) Then
components.Dispose ( )
End If
End If
MyBase.Dispose ( disposing )
End Sub
'Windows 窗體設(shè)計器所必需的
Private components As System.ComponentModel.IContainer
'注意:以下過程是 Windows 窗體設(shè)計器所必需的
'可以使用 Windows 窗體設(shè)計器修改此過程。
'不要使用代碼編輯器修改它。
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
<System.Diagnostics.DebuggerStepThrough ( ) > 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 Region
Private Sub Button1_Click ( ByVal sender As System.Object ,
ByVal e As System.EventArgs ) Handles Button1.ClickDim MyService As New localhost.Service1 ( )
DataGrid1.DataSource = MyService.Binding ( )
DataGrid1.DataMember = "Cust"
End Sub
Private Sub Button2_Click ( ByVal sender As System.Object ,
ByVal e As System.EventArgs ) Handles Button2.ClickDim MyService As New localhost.Service1 ( )
Dim ds As DataSet = DataGrid1.DataSource
Dim dsChanges As DataSet = ds.GetChanges ( )
If Not ( dsChanges Is Nothing ) Then
ds.Merge ( MyService.Update ( dsChanges ) , True )
End If
End Sub
End Class
關(guān)于“VB.NET如何調(diào)用Web Service”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
文章題目:VB.NET如何調(diào)用WebService
當(dāng)前鏈接:http://muchs.cn/article30/pdgsso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計公司、網(wǎng)站改版、搜索引擎優(yōu)化、服務(wù)器托管、網(wǎng)站設(shè)計、外貿(mào)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)