抓取正規化資料庫資料

學會了如何自已寫程式的時候又發現當資料量大的時候

會去google找說如何正規化~

正規化後的資料怎麼抓呢

相信對於剛接觸ASP.NET的人來說

都會使用內建的控制項來抓資料~

但到了比較熟了以後當然會想說自已來寫程式來抓資料

畢竟有些網頁不大適合使用這些控制項

學會了如何自已寫程式的時候又發現當資料量大的時候

會去google找說如何正規化~

正規化後的資料怎麼抓呢

現在就來介紹一下囉!

這邊範例是使用access(沒辦法筆者窮~><)

資料庫會有兩個資料表

一、program

主要還是id要有的

二、pdate

這邊會有主要的是id跟pid

pid就是program的id

順便一提我的program放的是課程因為課程大多都是固定的

而課程會有好幾次,所以我將課程的日期放入pdate裡

 

01 <table border="2" width="750" bordercolor="#000000" align="center" cellspacing="0"  cellpadding="8">
02                 <tr>
03                     <td width="175" height="34" align="center" bordercolor="#000000" bgcolor="#FECCCB">
04         <font size="2">課程名稱</font></td>
05                    <td width="109" height="34" align="center" bordercolor="#000000" bgcolor="#FECCCB">
06         <font size="2">時間</font></td>
07                    <td bordercolor="#000000" bgcolor="#FECCCB" height="34" width="290"  align="center">
08         <font size="2">課程大綱</font></td>
09                    <td width="98" height="34" align="center" bordercolor="#000000" bgcolor="#FECCCB">
10         <font size="2">備註</font></td>                  
11         
12                                       
13                 </tr>
14     <%string strDbCon = "Provider=Microsoft.jet.OLEDB.4.0;" + "Data Source="+ Server.MapPath("App_Data\\course.mdb");
15         //建立資料庫連結
16         OleDbConnection objCon = new OleDbConnection (strDbCon);
17         objCon.Open();
18         string strSQL = "SELECT * FROM program";
19         OleDbCommand objCmd = new OleDbCommand(strSQL, objCon);
20         OleDbDataReader objDataReader = objCmd.ExecuteReader();
21         //讀取資料
22         while (objDataReader.Read())
23         {
24             //建立正規化資料表的連結
25             string strSQL2 = "SELECT * FROM pdate where pid="+objDataReader[0];
26             OleDbCommand objCmd2 = new OleDbCommand(strSQL2, objCon);
27             OleDbDataReader objDataReader2 = objCmd2.ExecuteReader();
28             Response.Write("<tr>");
29             Response.Write("<td width='175'><font size='2'>【" + objDataReader[1] + "】<br>" + objDataReader[2] + "</font></td>");
30             Response.Write("<td width='109'><font size='2' face='Franklin Gothic Demi Cond'>");
31             //讀取正規化資料表的資料
32             while (objDataReader2.Read())
33             {
34                 //判斷資料是否為null
35                 if (!objDataReader2.IsDBNull(2))
36                 {
37                     
38                     DateTime mytime1 = Convert.ToDateTime(objDataReader2[2]);
39                     DateTime mytime2 = Convert.ToDateTime(objDataReader2[3]);
40
41                                
42                 if (mytime1.ToString("dd") == mytime2.ToString("dd"))
43                 {
44                     Response.Write(mytime1.ToString("M/d") );
45                 }

46                 else
47                 {
48
49                     Response.Write(mytime1.ToString("M/dd-") + mytime2.ToString("dd") );
50                 }

51             }

52                 if (objDataReader2.IsDBNull(4))
53                 {
54                     Response.Write("<br/>");
55                 }

56                 else
57                 {
58                     string memo = Convert.ToString(objDataReader2[4]);
59                     Response.Write("("+memo+")<br/>");
60                 }

61             }

62             
63             objDataReader2.Dispose();
64             objDataReader2.Close();
65             
66             Response.Write("</font></td>");
67             Response.Write("<td width='290'><font size='2'>" + objDataReader[3] + "</font></td>");
68             if(objDataReader.IsDBNull(10)){
69                 Response.Write("<td width='98'>.</td>");
70             }

71             else{
72                 Response.Write("<td width='98'><font size='2'>" + objDataReader[10] + "</font></td>");
73             }

74             Response.Write("</tr>");
75             i++;
76         }

77         objDataReader.Dispose();
78         objDataReader.Close();
79         objCon.Dispose();
80         objCon.Close();
81         %>
82         </table>

p.s 明明等一下就要出門了~硬是要寫一篇~~XD