asp开发中的面向对象化2009-12-12开发网站其实就是对数据库中数据的操作,所以我们可以把数据看作一个总的对象,每一个数据表都是一个对象,加入有数据表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