開發網站其實就是對資料庫中資料的操作,所以我們可以把資料看作一個總的對象,每一個資料表都是一個對象,加入有資料表Topic的話,我們可以這樣來做:
(以下部分只是一個思路,裡面的許多東西都在另外幾個類中,如cms,data之類的,大家看看思路就行,不明白的可以問我)
topic表的欄位假設為 ID,Title,Content,Hits,AddTime
[Copy to clipboard]CODE:
<%
'==================== 資料類: Topic_Class 注釋開始 ==========================
'== Important :對資料表 Topic 進行相關操作
'== Written by :Hankx Chen
'== Version : 1.3
'== Created Date :2007-2-6 17:04:40
'==================== 資料類: Topic_Class 注釋結束 ==========================
Class Topic_Class
'以下是系統資料欄位
Public ID,Title,Content,Hits,AddTime
'以下是自訂的公用屬性
Public Template
'初始化類
Private Sub Class_Initialize
AddTime=Now()
End Sub
'登出類
Private Sub Class_Terminate
End Sub
'Read方法用於讀取具體的資料內容
Public Sub Read()
on error resume next
cms.errorcode=0
cms.data.sql="Select Top 1 * From [Topic] where ID="&ID
Set cms.data.rs=cms.data.execute(cms.data.sql)
If Not cms.data.rs.eof Then
ID=cms.data.rs("ID")
Title=cms.data.rs("Title")
Content=cms.data.rs("Content")
Hits=cms.data.rs("Hits")
AddTime=cms.data.rs("AddTime")
Else
cms.errorcode=10000
cms.errortext="ReadDataError: 未能在資料表 [Topic] 找到 ID 為 "&ID&" 的資料"
End If
cms.data.rs.close
Set cms.data.rs=nothing
If err Then
cms.errorcode=10010
cms.errortext="ReadDataError: 從資料表 [Topic] 讀取 ID 為 的資料時出現錯誤:"&Err.Description
End IF
End Sub
'Save方法用於儲存添加的資料內容
Public Sub Save()
on error resume next
cms.errorcode=0
If Not IsObject(cms.data.conn) Then cms.data.open()
Set cms.data.rs = Server.CreateObject("ADODB.Recordset")
cms.data.sql="Select Top 1 * From [Topic]"
cms.data.rs.open cms.data.sql,cms.data.conn,1,3
cms.data.rs.Addnew
cms.data.rs("ID")=ID
cms.data.rs("Title")=Title
cms.data.rs("Content")=Content
cms.data.rs("Hits")=Hits
cms.data.rs("AddTime")=AddTime
cms.data.rs.update
cms.data.rs.close
Set cms.data.rs=nothing
If err Then
cms.errorcode=10020
cms.errortext="SaveDataError: 向資料表 [Topic] 添加資料時出現錯誤:"&Err.Description
End IF
End Sub
'Update方法用於更新具體的資料內容
Public Sub Update()
on error resume next
cms.errorcode=0
If Not IsObject(cms.data.conn) Then cms.data.open()
Set cms.data.rs = Server.CreateObject("ADODB.Recordset")
cms.data.sql="Select Top 1 * From [Topic] where ID="&ID
cms.data.rs.open cms.data.sql,cms.data.conn,1,3
If Not cms.data.rs.Eof Then
cms.data.rs("ID")=ID
cms.data.rs("Title")=Title
cms.data.rs("Content")=Content
cms.data.rs("Hits")=Hits
cms.data.rs("AddTime")=AddTime
cms.data.rs.update
Else
cms.errorcode=10000
cms.errortext="UpdateDataError: 在資料表 [Topic] 中無法找到 ID 為 "&ID&" 的資料"
End If
cms.data.rs.close
Set cms.data.rs=nothing
If err Then
cms.errorcode=10030
cms.errortext="UpdateDataError: 向資料表 [Topic] 更新資料時出現錯誤:"&Err.Description
End IF
End Sub
'Delete方法用於刪除具體的資料內容
Public Sub Delete()
on error resume next
cms.errorcode=0
If Not IsObject(cms.data.conn) Then cms.data.open()
Set cms.data.rs = Server.CreateObject("ADODB.Recordset")
cms.data.sql="Select Top 1 * From [Topic] where ID="&ID
cms.data.rs.open cms.data.sql,cms.data.conn,1,3
If Not cms.data.rs.eof Then
cms.data.rs.delete
cms.data.rs.update
Else
cms.errorcode=10000
cms.errortext="DeleteDataError: 在資料表 [Topic] 中無法找到 ID 為 "&ID&" 的資料"
End If
cms.data.rs.close
Set cms.data.rs=nothing
If err Then
cms.errorcode=10040
cms.errortext="DeleteDataError: 從資料表 [Topic] 中刪除 ID 為 "&ID&" 資料時出現錯誤:"&Err.Description
End IF
End Sub
'List函數用於返回模版列表
Public Function List()
End Function
'Hit用於點擊
Public Sub Hit()
End Sub
End Class
%>