本文有兩個小範例,都是用DataSet來作。
第一個範例 跟上一篇文章(#4)完全相同,
差別只是把 Web Service裡面的 ADO.NET (DataReader)的寫法,
修改成 SqlDataSource來作而已(但從頭到尾都要自己手寫程式),SqlDataSource的DataMode設定為「DataSet」。
第二,文末附上另一個範例(Yahoo知識+的回答),Web Service將會傳回一個DataSet。
..................................................................................................................................................
如果讀者尚未具備上一篇文章的相關知識,請立刻暫停!
貿然看下去,可能會消化不良。
萬丈高樓平地起。
總要先學會走路,才來練習跑步、跳遠、跳高.....對嗎?
..................................................................................................................................................
本文相關知識,請參考以前的文章 ----
這個程式,我就省略一些步驟了。
當成給各位讀者的「HomeWork」,自己補完。
我只提供最核心的Web Service作法,
其餘的步驟,請參考上一篇文章的「第二步驟(加入Web參考)」&「第三步驟(ASP.NET呼叫這個Web Service)」。
===============================================================================
檔名 Service_4.asmx
以下是 Service_4.vb的程式內容。
註:淡灰色的字體,都是VS 2005/2008幫我們事先做好的宣告&設定等等......
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
'----自己寫的----
Imports System
Imports System.Data
Imports System.Data.SqlClient
'----自己寫的----
' 若要允許使用 ASP.NET AJAX 從指令碼呼叫此 Web 服務,請取消註解下一行。
' <System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class Service_4
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function Get_Title(ByVal u_id As Integer) As String
Dim u_Title As String
'--------------------------------------------------
'----- 手動撰寫 SqlDataSource -----
'--------------------------------------------------
Dim SqlDataSource1 As New SqlDataSource
'==自己手動撰寫 SqlDataSource ,必須先寫下面三行 ==
'== 1.資料庫的連線字串 ConnectionString ==
SqlDataSource1.ConnectionString = "Data Source=ACER_4105NB;Initial Catalog=test;Persist Security Info=True;User ID=test;Password=test"
'== 2.撰寫SQL指令 ==
SqlDataSource1.SelectCommand = "select title from test where id = " & u_id
'== 3.執行SQL指令 .select() / [DataSet 版]==
SqlDataSource1.DataSourceMode = SqlDataSourceMode.DataSet
'== 如果 DataSourceMode 屬性設為 DataSet 值,則 Select 方法會傳回 DataView 物件。
Dim args As New DataSourceSelectArguments
Dim dv As DataView = SqlDataSource1.Select(args)
u_Title = dv.Table.Rows(0).Item("title").ToString()
dv.Dispose()
SqlDataSource1.Dispose()
Return u_Title '---- 傳回值 ----
End Function
End Class
上面這些程式碼,我更早已前就發表在BLOG上面了,不信??? 請看這篇文章 ---- http://www.dotblogs.com.tw/mis2000lab/archive/2008/09/22/sqldatasource_select_0922.aspx
(上面的文章,就是用SqlDataSource來作,還分成兩支,分別完成DataReader模式、DataSet模式。)
...................................................................................................................................................................................................
2008/9/28補充: 第二個範例
剛剛在Yahoo知識+有人發問,我隨手回答一下。
如果您的Web Service要傳回一個DataSet,該怎麼作?
http://tw.knowledge.yahoo.com/question/question?qid=1008092804860
答案如下,僅供參考:
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
'----自己寫的----
Imports System
Imports System.Data
Imports System.Data.SqlClient
' 若要允許使用 ASP.NET AJAX 從指令碼呼叫此 Web 服務,請取消註解下一行。
' <System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class Service_5_DataSet
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function Get_DataSet(ByVal u_id As Integer) As DataSet
Dim u_DataSet As New DataSet
Dim Conn As New SqlConnection("連結資料庫的字串、帳號與密碼")
Dim myAdapter As SqlDataAdapter
myAdapter = New SqlDataAdapter("SQL指令,請自己寫", Conn)
myAdapter.Fill(u_DataSet, "test")
Return u_DataSet
End Function
End Class
' 註解:除了粗體字要自己寫程式以外,其他都是VS 2008自動產生的宣告。
雖然 ASP.NET 2.0以後,推出 SqlDataSource / AccessDataSource,真的很強、很好用。
但寫程式,不可能永遠都用現成的精靈、靠GridView / ListView就完成案子。
當客戶要求更多.........,最終還是要瞭解原理,自己動手作!
這時候,自己動手寫寫 ADO.NET程式,搭配其他控制項,甚至是傳統的HTML表單來作。
程式的變化會更多彩多姿。
這也就是我為什麼在書本裡面,放了兩章「其他ASP.NET比較少講的」ADO.NET觀念 & ADO.NET手寫程式
學無止境啊!
只會拉拉控制項、設定幾個精靈畫面,對於初學者來說,夠了!
但想要要進入業界,還有一段距離。
希望我的書,能協助各位讀者順利地走向另一段...........(基礎不穩的初學者,下面這本書記得要買!
)
2008/10/2補充:微軟MSDN的文章 ---- HOW TO:使用 WebMethod 屬性
2009/1/14補充: Web Service系列的下一篇文章:Web Service入門 #6,統一管理帳號、密碼的登入
---------------------------------------------------------------------------------------------------------------------------------------------------
關於本網站的 Web Service,已經發表一系列文章,
請看:http://www.dotblogs.com.tw/mis2000lab/Tags/Web%20Service/default.aspx
2009/11/10補充: 本系列 Web Service文章已經延伸許多範例與程式,並撰寫成書本裡面的一章,
將會發表在這一本書裡面,請看:[預告]ASP.NET專題實務(下集)-- 範例集與.NET 4.0新功能
剛剛才發現,這是我在點部落的第兩百篇文章!
賀!「兩百」文達成!
距離上一次的第一百篇文章(6/23發表,http://www.dotblogs.com.tw/mis2000lab/archive/2008/06/23/4362.aspx),差距三個月~
今日值班正妹,不知道她是誰?常常在 MOMO富邦購物台見到她,一個模特兒。

...... 寄信給我 mis2000lab (at) 雅虎.com.台灣 ................................................................................................................
ASP.NET專題實務
(文魁出版,VB版 P8187 / C#版P09027)
下集已經出版囉~~~ASP.NET專題實務II:範例應用與4.0新功能 
.............................................................................................................. 寄信給我 mis2000lab (at) 雅虎.com.台灣 ........