1 / 27

C# 與網路資料庫程式設計

C# 與網路資料庫程式設計. 羅東高中邱柏翰 bohann@ltsh.ilc.edu.tw. 大綱. 本專題結合網頁設計、資料庫、 C# 程式語言等資訊技術 希望學生能做出一個線上資料庫網站 為了與校園生活結合,本專題的主題為 — 校園公佈欄系統。. 歡迎使用學科中心教材. C# 相關研發教材. 資訊學科中心出版之「 98 教學資源小組研發教材手冊」「教學活動設計參考範例- Visual C# 程式設計 ( 高慧君 老師 ) 」 先使用「主控台應用程式」來介紹程式設計的基本概念,再使用「 Windows Form 應用程式」來進行專題:製作一個 mp3 鬧鐘程式。.

Download Presentation

C# 與網路資料庫程式設計

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C#與網路資料庫程式設計 羅東高中邱柏翰 bohann@ltsh.ilc.edu.tw

  2. 大綱 • 本專題結合網頁設計、資料庫、C#程式語言等資訊技術 • 希望學生能做出一個線上資料庫網站 • 為了與校園生活結合,本專題的主題為—校園公佈欄系統。

  3. 歡迎使用學科中心教材

  4. C#相關研發教材 • 資訊學科中心出版之「98教學資源小組研發教材手冊」「教學活動設計參考範例-Visual C# 程式設計 (高慧君老師)」 • 先使用「主控台應用程式」來介紹程式設計的基本概念,再使用「Windows Form應用程式」來進行專題:製作一個mp3鬧鐘程式。

  5. C#相關研發教材 • 資訊學科中心出版之「100教學資源小組研發教材手冊」教學活動設計參考範例-Visual C# 程式設計(李啟龍教師)」 • 主控台應用程式介紹、視窗應用程式介紹

  6. ACCESS相關研發教材 • 資訊學科中心出版之「98教學資源小組研發教材手冊」教學活動設計參考範例-資料庫應用與實作(張啟中教師)」 • 以「校園圖書館借閱系統」為範例,逐一引導學生認識資料庫系統及其操作方式

  7. 課程安排(8節) • (1節)建置專題所需的環境 • (2節)設計專題所需的資料庫、練習SQL語法 • (2節)使用ASP.NET程式語言操作Access資料庫(第一部份) • (2節)使用ASP.NET程式語言操作Access資料庫(第二部分) • (1節)設定安全性,完成專題

  8. .NET Framework

  9. .NET Framework由兩大元件所構成—共通語言執行環境(Common Language Runtime, CLR)和.NET Framework類別庫。 • 1. 共通語言執行環境:是.NET應用程式所可以共通使用的執行引擎。在.NET Framework上可以使用Visual Basic、C#、J#、C++、JScript等程式語言,而用這些程式語言所寫成的應用程式都可以在CLR上運行(轉成Microsoft Intermediate Language共同中間語言),只要您把.NET Framework環境準備好就可以了。 • 2. .NET Framework類別庫:支援.NET的各種語言可以共通使用的標準類別庫

  10. 工具安裝 • Visual Studio 2008 Express內包括:Visual C#、Visual Basic、Visual C++、Visual Web Developer,需要逐一安裝,本專題需要安裝第四個—Visual Web Developer。

  11. 專題摘要 • 本專題將會寫出一個互動式網頁 • 本專題用Visual Studio 2008 Express其中的Visual Web Developer 2008 Express來設計互動式網頁 • Visual Web Developer 2008 Express在開發ASP .NET網站的時候,可以選擇用Visual C#或是Visual Basic語言,本專題使用Visual C# • 本專題資料庫檔案以Access 2003製作,以利學生和先前所學連結

  12. 資料庫的觀念

  13. 如果要做網路、資料庫的專題 • ASP、PHP比較常見,資料比較多

  14. ASP的寫法 <% set adocon=Server.CreateObject("ADODB.Connection") adocon.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.Mappath("db.mdb") '有資料傳回的時候用這種寫法,「有傳回值」 sqlstr = "SELECT * FROM 資料表" set rs = adocon.Execute(sqlstr) '可以直接開一個table當作資料的來源,「有傳回值」 ' set rs = adocon.Execute("資料表") '如果「有」資料傳回的時候 ' rs.Open sqlstr, ActiveConnection, CursorType, LockType, Options '如果「沒有」資料傳回的時候,例如新增、修改、刪除 ' adocon.Execute "用來資料異動的SQL指令" %>

  15. <% Response.Write("<table border='1'>") '輸出屬性 Response.Write("<tr>") For j=0 to rs.Fields.Count -1 Response.Write("<td>") Response.Write rs.Fields(j).name Response.Write("</td>") NEXT Response.Write("</tr>") '一筆一筆輸出資料 While not rs.EOF Response.Write("<tr>") For j=0 to rs.Fields.Count -1 Response.Write("<td>") Response.Write rs.Fields(j).value Response.Write("</td>") NEXT Response.Write("</tr>") rs.Movenext Wend Response.Write("</table>") %>

  16. Response.Write("<tr>")+chr(13)+chr(10) Response.Write("<TD width='17' bgcolor='#CCFFFF' height='17'><img border='0' src='ab.jpg' width='20' height='17'></TD>") Response.Write("<td bgcolor='#CCFFFF'><a href=""javascript:newin(530, 500, 'view.asp?ID="&rs("ID")&"')""><font size='2'>") temp=rs.Fields("活動名稱").value Response.Write temp Response.Write("</a></font></td>")+chr(13)+chr(10) Response.Write("<td bgcolor='#CCFFFF'><font size='2'><p align='center'>") If rs.Fields("活動內外").value="校外" Then Response.Write "<p align='center'>" & rs.Fields("主辦單位").value Else Response.Write "<p align='center'>" & rs.Fields("活動內外").value End If

  17. 上機實作 • 建立網站—加入資料庫—新增、修改、刪除

  18. 建立新網站

  19. 加入資料庫Access檔案

  20. CodeBehind using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } } <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> </div> </form> </body> </html> Default.aspx.cs Default.aspx

  21. 用工具拉出來的程式碼 <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/db.mdb" SelectCommand="SELECT * FROM [公佈欄]"> </asp:AccessDataSource> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="新聞編號" DataSourceID="AccessDataSource1"> <Columns> <asp:BoundField DataField="公告處室" HeaderText="公告處室" SortExpression="公告處室" /> <asp:BoundField DataField="公告日期" HeaderText="公告日期" SortExpression="公告日期" /> <asp:BoundField DataField="公告主題" HeaderText="公告主題" SortExpression="公告主題" /> <asp:HyperLinkField DataNavigateUrlFields="新聞編號" DataNavigateUrlFormatString="detail.aspx?id={0}" DataTextField="公告主題" HeaderText="公告主題" /> </Columns> </asp:GridView> 程式在default.aspx當中

  22. 元件之間的關係 資料來源:奚江華(2010)。ASP.NET4.0 完美入門:使用C#。台北市:碁峰資訊。

  23. ADO .NET的物件模型 資料來源:陳會安(2008)。ASP.NET3.5:網頁製作徹底研究:使用C#。台北市:旗標。

  24. 手工打造也是可以的 string strDbCon, strSQL; strDbCon="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("App_Data\\db.mdb") ; OleDbConnection objCon; OleDbCommand objCmd; OleDbDataReader objDR; objCon = new OleDbConnection(strDbCon); objCon.Open(); strSQL = "Select * From 公佈欄"; objCmd = new OleDbCommand(strSQL, objCon); objDR = objCmd.ExecuteReader(); if (objDR.HasRows) { while (objDR.Read()) { Label1.Text += objDR["公告日期"] + "-"; Label1.Text += objDR["公告主題"] + "-"; Label1.Text += objDR["公告內容"] + "<br>"; } }

  25. 實際操作 • 建立網站與公告欄畫面 • 比較詳盡的「新增」請參閱教材 • 「修改」「刪除」功能用內建的應該可以滿足需求 • 安全性請參閱教材

  26. 參考資源 • MIS2000 Lab、周棟祥、吳進魯(2010)。ASP.NET 4.0專題實務:使用C#。台北市:松崗資產管理。 • 資訊教育研究室(2010)。Visual C# 2008從零開始學習手冊。台北縣:文魁行銷。 • 山田祥寬(2007)。學會ASP .NET 2.0的關鍵課程。台北市:旗標。 • 資訊學科中心出版之「98教學資源小組研發教材手冊」「教學活動設計參考範例-Visual C# 程式設計 (高慧君老師)」 • 資訊學科中心出版之「100教學資源小組研發教材手冊」教學活動設計參考範例-Visual C# 程式設計(李啟龍教師)」

  27. 推薦書籍

More Related