Web Service入門 #4,搭配 ADO.NET DataReader的範例

這是Web Service的第四個範例。
我們用 Web Service來寫 ADO.NET程式,從資料庫撈點資料來玩玩。

把舊有的功能,加上「Web Service」的新方法來實作。
是不是更能瞭解 Web Service帶來什麼改變呢?

 


 

 

 

這一篇文章,將介紹 Web Service來作簡單的資料庫存取(ADO.NET),需要一些基本的認知,

(9vs1.com可單獨購買此課程) Web Service + Web API  https://9vs1.com/go/?i=83d6700ef666

 

關於 Web Server,建議看過下面的三篇文章後,才能具備基礎知識:

關於ADO.NET的 DataReader作法,請看過下列文章,才能具備基本知識:

 

如果讀者尚未具備上述兩大知識,貿然看下去,可能會消化不良。

萬丈高樓平地起。        總要先學會走路,才來練習跑步、跳遠、跳高.....對嗎?

 

第一,撰寫 Web Server

大部分的 Web Server程式、宣告,都是VS 2008幫我們自動完成的。 

有了VS 2005/2008這麼強的開發工具,寫程式已經很簡單囉!

01 Imports System.Web
02 Imports System.Web.Services
03 Imports System.Web.Services.Protocols

04
05 ' 若要允許使用 ASP.NET AJAX 從指令碼呼叫此 Web 服務,請取消註解下一行。
06 ' <System.Web.Script.Services.ScriptService()> _
07 <WebService(Namespace:="http://tempuri.org/")> _
08 <WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
09 <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
10 Public Class Service_3
11      Inherits System.Web.Services.WebService
12
13     <WebMethod()> _
14     Public Function 函數名稱(輸入值1)as 傳回值
15     
16     End Function
17
18 End Class

 

真正的程式如下!這邊將會使用 ADO.NET的程式 (for DataReader),全部自己動手寫。

採用的 test資料表,就是本書提供的範例。

01 Imports System.Web
02 Imports System.Web.Services
03 Imports System.Web.Services.Protocols
04
05 '----自己寫的----
06 Imports System
07 Imports System.Data
08 Imports System.Data.SqlClient

09 '----自己寫的----
10
11 ' 若要允許使用 ASP.NET AJAX 從指令碼呼叫此 Web 服務,請取消註解下一行。
12 ' <System.Web.Script.Services.ScriptService()> _
13 <WebService(Namespace:="http://tempuri.org/")> _
14 <WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
15 <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
16 Public Class Service_3
17      Inherits System.Web.Services.WebService
18
19     <WebMethod()> _
20     Public Function Get_Title(ByVal u_id As Integer) As String
21
22         Dim Conn As SqlConnection = New SqlConnection
23         Conn.ConnectionString = "Data Source=ACER_4105NB;Initial Catalog=test;Persist Security Info=True;User ID=test;Password=test"
24
25         Dim dr As SqlDataReader = Nothing
26
27         Dim cmd As SqlCommand = New SqlCommand("select title from test where id = " & u_id, Conn)
28
29         Dim u_Title As String
30
31         Try
32             Conn.Open()   '---- 1. 連結DB
33             dr = cmd.ExecuteReader()   '---- 2. 執行SQL指令,取出資料
34
35             dr.Read()
36             u_Title = dr.Item("title").ToString
37
38         Catch ex As Exception
39             u_Title = "Exception(錯誤訊息)----   " + ex.ToString()
40         Finally
41             '---- Always call Close when done reading.
42             If Not (dr Is Nothing) Then
43                 cmd.Cancel()
44                 dr.Close()
45             End If
46
47             '---- Close the connection when done with it.
48             If (Conn.State = ConnectionState.Open) Then
49                 Conn.Close()
50                 Conn.Dispose()  
51             End If
52         End Try
53
54
55         Return u_Title  '---- 傳回值
56     End Function

57
58 End Class

 

(9vs1.com可單獨購買此課程) Web Service + Web API  https://9vs1.com/go/?i=83d6700ef666

 

第二,加入「Web參考」 ,名為 localhost.Service_3

           這部份不再贅述,請看上方的第二篇文章 ---- Web Server #2

 

第三,撰寫一支 ASP.NET程式,「呼叫」Web Service來用。

HTML畫面很簡單,只有一個 TextBox、 Button與 Label控制項。

HTML畫面的原始碼如下:

01     <form id="form1" runat="server">
02     <p>
03         您想看哪一篇文章,請輸入文章ID編號 :
04         <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
05         &nbsp;&nbsp;
06         <asp:Button ID="Button1" runat="server" Text="輸  入" />
07     </p>
08     <div>
09     
10         文章標題(Title):  
11         <asp:Label ID="Label1" runat="server" style="color: #FF0000"></asp:Label>
12     
13     </div>
14     </form>

  =====================================================================================

後置程式碼(Code Behind)如下:

   Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim ws As New localhost.Service_3

            Dim input_int As Integer = CType(TextBox1.Text, Integer)

                      Label1.Text = ws.Get_Title(input_int)

    End Sub

 

 執行成果:    

(9vs1.com可單獨購買此課程) Web Service + Web API  https://9vs1.com/go/?i=83d6700ef666

 

 

 

這是一個很簡單的程式,

把舊有的功能,加上「Web Service」的新方法來實作。

是不是更能瞭解 Web Service帶來什麼改變呢?

 

如果我把上面的 Web Service開放出來,那麼全世界的程式設計師都能使用我這個Function來獲得結果。

這就是 Web Service有趣、有用的地方!

 

 

看了這三篇文章,我們以「實作」代替繁雜的理論,

從完全不會,到現在寫程式,不就是一下子的時間而已嗎?

 

不要給自己那麼多藉口,說什麼「我數學不好、我邏輯不好,所以我不可能學會寫程式!

說真的,程式寫不好,是你運氣不好而已............

可能是遇見了不適合自己的「書本」、遇上了教學方法不適合自己的「老師」,

千萬不要對自己失望!不要放棄!

 

多給自己一次機會,每個人都可以當程式設計師,領一份不錯的薪資!

只要努力、多練習、多看書(看別人的範例)...........肯認真練習!假以時日,就會成為武林高手

 

 

---------------------------------------------------------------------------------------------------------------------------------------------------

 關於本網站的 Web Service,已經發表一系列文章,

請看 https://dotblogs.com.tw/mis2000lab/Search?q=Web+Service 

 

 今日值班正妹,張虹(個人專訪:www.carsfans.com/display.asp?keyno=589

 

 

 

 

 

 

 

 

 

我將思想傳授他人, 他人之所得,亦無損於我之所有;

猶如一人以我的燭火點燭,光亮與他同在,我卻不因此身處黑暗。----Thomas Jefferson

線上課程教學,遠距教學 (Web Form 約 51hr)  https://dotblogs.com.tw/mis2000lab/2016/02/01/aspnet_online_learning_distance_education_VS2015

線上課程教學,遠距教學 (ASP.NET MVC 約 135hr)  https://dotblogs.com.tw/mis2000lab/2018/08/14/ASPnet_MVC_Online_Learning_MIS2000Lab

 

寫信給我,不要私訊 --  mis2000lab (at) yahoo.com.tw  或  school (at) mis2000lab.net

 (1) 第一天 ASP.NET MVC5 完整影片(5.5小時 / .NET 4.x版)免費試聽。影片 https://youtu.be/9spaHik87-A 

 (2) 第一天 ASP.NET Core MVC 完整影片(3小時 / .NET Core 6.0~8.0)免費試聽。影片 https://youtu.be/TSmwpT-Bx4I 

[學員感言] mis2000lab課程評價 - ASP.NET MVC , WebForm  。 https://mis2000lab.medium.com/%E5%AD%B8%E5%93%A1%E6%84%9F%E8%A8%80-mis2000lab%E8%AA%B2%E7%A8%8B%E8%A9%95%E5%83%B9-asp-net-mvc-webform-77903ce9680b  


ASP.NET遠距教學、線上課程(Web Form + MVC)。 第一天課程, "完整" 試聽。 

.........   facebook社團   https://www.facebook.com/mis2000lab   ......................

.........  YouTube (ASP.NET) 線上教學影片  https://www.youtube.com/channel/UC6IPPf6tvsNG8zX3u1LddvA/

 

Blog文章 "附的範例" 無法下載,請看 https://dotblogs.com.tw/mis2000lab/2016/03/14/2008_2015_mis2000lab_sample_download

請看我們的「售後服務」範圍(嚴格認定)。

...................................................................................................................................................... 

ASP.NET MVC  => .NET Core MVC 線上教學  ...... 第一天課程 完整內容 "免費"讓您評估 / 試聽

[遠距教學、教學影片] ASP.NET (Web Form) 課程 上線了!MIS2000Lab.主講   事先錄好的影片,並非上課側錄!   觀看時,有如「一對一」面對面講課