close
大量資料的排序與查詢
今天要教大家的是如何處理大量資料的排序與查詢,筆者選擇這個題目的原因主要是因為前幾天本公司業務拿了一份新浪網(Sina)上個月的廣告統計表給筆者,由於新浪網的業務量大,廣告統計表的資料量相對的也非常的大,令筆者在觀看時非常不便,甚至要找到某家廣告商的資料都很難,我相信這份廣告統計表如果交到廣告商的手上也會令廣告商非常頭痛,因為這份資料包含的欄位有廣告商名稱、廣告放置頁面、廣告放置起始時間、廣告放置截止時間、廣告被瀏覽次數、廣告被點選次數、廣告點選率等等,於是筆者就在想如果能夠讓廣告商上網查閱這些資料,而且能夠對這些資料進行排序與搜尋的功能不但能夠減少紙張的浪費(現在大家都在提倡環保),而且也方便廣告商的觀看,廢話不多說馬上就讓我們進入程式的撰寫,一開始我們也是先建立一個存放資料的資料庫,我們把這個資料庫取名為Companyx.mdb資料庫欄位定義如下

tblEmployees
ID自動編號
Name文字
Title文字
Department文字

接著讓我們看看程式內容並為大家解說:我們把這個程式名為Sort_search.asp
Sort_search.asp


<%'指定資料庫路徑%>

<% Session("DatabasePath") = "C:\inetpub\wwwroot\asp\search\companyx.mdb" %>

<% 

'-- 宣告變數


Dim DataConnection, cmdDC, RecordSet

Dim SearchText

SValue = Request("SValue")

SValue1 = Request("SValue1")

SearchText = Request.Form("txtSearchText") 


'-- 建立資料庫物件與開啟資料庫連結


Set DataConnection = Server.CreateObject("ADODB.Connection")


DataConnection.Open "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" & Session("DatabasePath") & ";"

Set cmdDC = Server.CreateObject("ADODB.Command")

cmdDC.ActiveConnection = DataConnection

'-- default SQL

SQL = "SELECT * FROM tblEmployees"


'排序的功能全部都以按鈕來完成

'如果是按下編號排序的按鈕則

'-- 列出資料時以編號作為排序索引

If Request.Form("btnSortID") = "升羃排序" Then


SQL = "SELECT tblEmployees.*, tblEmployees.ID FROM tblEmployees ORDER BY
tblEmployees.ID ASC;"

End If

If Request.Form("btnSortID") = "降羃排序" Then

SQL = "SELECT tblEmployees.*, tblEmployees.ID FROM tblEmployees ORDER BY tblEmployees.ID DESC;"

End If


'如果是按下姓名排序的按鈕則

'-- 列出資料時以姓名作為排序索引

If Request.Form("btnSortName") = "升羃排序" Then

SQL = "SELECT tblEmployees.*, tblEmployees.Name FROM tblEmployees ORDER BY tblEmployees.Name ASC;"

End If
If Request.Form("btnSortName") = "降羃排序" Then

SQL = "SELECT tblEmployees.*, tblEmployees.Name FROM tblEmployees ORDER BY tblEmployees.Name DESC;"

End If


'如果是按下職銜排序的按鈕則

'-- 列出資料時以職銜作為排序索引

If Request.Form("btnSortTitle") = "升羃排序" Then

SQL = "SELECT tblEmployees.*, tblEmployees.Title FROM tblEmployees ORDER BY tblEmployees.Title ASC;"


End If
If Request.Form("btnSortTitle") = "降羃排序" Then

SQL = "SELECT tblEmployees.*, tblEmployees.Title FROM tblEmployees ORDER BY tblEmployees.Title DESC;"

End If


'如果是按下部門排序的按鈕則

'-- 列出資料時以部門作為排序索引


If Request.Form("btnSortDepartment") = "升羃排序" Then


SQL = "SELECT tblEmployees.*, tblEmployees.Department FROM tblEmployees ORDER BY tblEmployees.Department ASC;"


End If


If Request.Form("btnSortDepartment") = "降羃排序" Then


SQL = "SELECT tblEmployees.*, tblEmployees.Department FROM tblEmployees ORDER BY tblEmployees.Department DESC;"

End If


'-- 如果是搜尋資料的話

If Request.Form("btnSearch") = "Search" Then

SQL = "SELECT * FROM tblEmployees WHERE " & SValue & " Like '%" & SearchText & "%' ORDER BY " & SValue & " " & SValue1 & ";"

End If

cmdDC.CommandText = SQL
Set RecordSet = Server.CreateObject("ADODB.Recordset")


'開啟資料庫集合時是以唯讀模式開啟且擁有最快回應時間

'cursortype = 1 locktype = 0

RecordSet.Open cmdDC, , 0, 1 

%>

<html>

<body>

<center>

<%'將所有資料與按鈕顯示出來%>

<form action="sort_search.asp" method="post">


<table border="1" cellpadding="0" cellspacing="0" width="500">

<tr>

<td align="center"><b>編號</b></td>

<td align="center"><b>姓名</b></td>

<td align="center"><b>職銜</b></td>

<td align="center"><b>部門</b></td>

</tr>

<tr><%'以按鈕的型式作參數傳遞的功能%>

<td align="center"><input type="submit" name="btnSortID" value="升羃排序"><input
type="submit" name="btnSortID" value="降羃排序"></td>

<td align="center"><input type="submit" name="btnSortName" value="升羃排序">
<input type="submit" name="btnSortName" value="降羃排序"></td>

<td align="center"><input type="submit" name="btnSortTitle" value="升羃排序"><input
type="submit" name="btnSortTitle" value="降羃排序"></td>

<td align="center"><input type="submit" name="btnSortDepartment" value="升羃排
序"><input type="submit" name="btnSortDepartment" value="降羃排序"></td>

</tr>

<%

'-- 移到資料頂端

If Not RecordSet.BOF Then

RecordSet.MoveFirst

'-- 列出資料直到資料庫底端

Do Until RecordSet.EOF



Response.Write("<tr>")

Response.Write(" <td>" & RecordSet.Fields("ID") & "</td>")

Response.Write(" <td>" & RecordSet.Fields("Name") & "</td>")

Response.Write(" <td>" & RecordSet.Fields("Title") & "</td>")

Response.Write(" <td>" & RecordSet.Fields("Department") & "</td>")

Response.Write("</tr>")

RecordSet.MoveNext

Loop

End If


%>

</table>

<p><%'顯示搜尋介面%>

<INPUT TYPE=TEXT NAME="txtSearchText" SIZE=10>

<SELECT NAME="SValue"><option VALUE="ID" SELECTED>編號</option>

<option VALUE="Name">姓名</option> 

<option VALUE="Title">職銜</option>

<option VALUE="Department">部門</option> 

</select>

<SELECT NAME="SValue1"><option VALUE="ASC" SELECTED>升羃排序</option>

<option VALUE="DESC">降羃排序</option> 


</select>

<input type="submit" name="btnSearch" value="Search"> 

</form>

</center>

</body>

</html>


<%

'-- 關閉資料庫


RecordSet.Close

Set RecordSet = Nothing

Set cmdDC = Nothing

DataConnection.Close

Set DataConnection = Nothing 

%>

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 11 的頭像
    11

    冠霖的部落格

    11 發表在 痞客邦 留言(0) 人氣()