自己制作的Asp数据库访问类。
<%
'-------------------------------------------------------------------------
'************************** 数据库-表-模版类 2.0 *************************
' 实现数据库中表TableName的 增 删 改 读单条记录 功能
'*************************************************************************
'私有变量:p_DbConnection:数据库连结,p_TableName:表名
'属性: DbConnString:数据库连结字符串
' Error:错误代码 ErrorDesc:错误信息 Version:版本信息
' Property1:字段1 Property2:字段2 Property3:字段3
' Property4:字段4 Property5:字段5 Property6:字段6
' Property7:字段7 Property8:字段8 Property9:字段9
'方法:
'DBOpen()
'DBClose()
'Insert() 直接将属性字段里面存储的信息用insert方法加入到数据库中
'AddNew() 直接将属性字段里面存储的信息用addnew方法加入到数据库中
'Delete() 用delete语句删除记录/需要打开数据库连接 参数:sql where 子句
'Update() 用update语句更新记录/需要打开数据库连接 参数:sql where 子句
'Fill() 用select语句取1条记录 /需要打开数据库连接 参数:sql where 子句
'
'使用方法:
'1 复制本模版到新文件,文件名按表名更改
'2 更改 有注释 [modify] 的部分
'3 更改 Property 为表中对应字段 (用replace,直接用字段名替换Property1,不要选择whole word only)
'4 调整各个字段的 get 和 let
'5 按需要处理错误代码
'
'---------------应用例子---------
'set tb1 = new TB_TableName ' 实例化
'tb1.DbOpen ' 打开db
'
'---addnew---
'tb1.Propeety 1="sdfsd" ' 向属性赋值
' . . .
'tb1.Propeety 9="sdfsd"
'tb1.addnew()
'
'---fill---
'tb1.fill() '
'
'---delete---
'tb1.delete() '
'
'---update---
'tb1.fill()
'tb1.Propeety1="sdfsd" 只update Propeety1的值
'tb1.update()
'----------------------------------------------------------
Class TB_TableName '[modify] 类名 更改为与表相关的名称
Private p_DbConnection,p_DbConnString '连接数据库变量
Private p_Rs,p_TableName '记录集,数据表名变量
Private p_Error,p_ErrorDesc(14) '错误信息
'-----下面的变量是数据表中的字段
Private p_Property1,p_Property2,p_Property3,p_Property4
Private p_Property5,p_Property6,p_Property7,p_Property8,p_Property9
'===============系统过程=========================
'--------------类初始化------------------
Private Sub Class_Initialize
On Error Resume Next
p_DbConnString = Application("DbConnString") 'Db连接字符串 [modify] 如果没有Application()变量直接输入连结字符串
p_TableName = "TableName" '[modify] 更改为数据表名称
p_Error = 0 '此值用于给用户返回错误信息 0 没错误 1-。。。下列对应错误
p_ErrorDesc(1) = "未知错误!"
p_ErrorDesc(2) = "数据库连结不成功!"
p_ErrorDesc(3) = "数据库关闭不成功!"
p_ErrorDesc(4) = "新增记录过程不成功!"
p_ErrorDesc(5) = "没有找到指定数据,无法更新数据!"
p_ErrorDesc(6) = "删除数据不成功!"
p_ErrorDesc(7) = ""
p_ErrorDesc(8) = "读取记录集不成功!"
p_ErrorDesc(9) = "没有找到要更新的记录!"
p_ErrorDesc(10) = "更新记录不成功!"
p_ErrorDesc(11) = "填充数据过程不成功!"
p_ErrorDesc(12) = "没有找到指定数据,无法填充数据!"
p_ErrorDesc(13) = ""
p_ErrorDesc(14) = ""
' p_Property = iniValue
End Sub
'--------------类销毁------------------
Private Sub Class_Terminate
On Error Resume Next
If isObject(p_DbConnection) Then '销毁数据库连结对象
p_DbConnection.close
set p_DbConnection = nothing
End If
If IsObject(p_Rs) Then '销毁Recordset对象
p_Rs.close
set p_Rs = nothing
End If
End Sub
'=============================
'----------属性: 返回版本号给用户-只读-------------------
Public Property Get Version
Version="DB Table Templete Class Version (1.0)"
Version=Version & "<BR>Made by Scott! Hbzhuxu@21cn.com"
End Property
'----------属性: 错误代码 返回错误代码给用户-只读--------
Public Property Get Error
Error=p_Error
End Property
'---------属性: 错误信息 返回错误说明给用户-只读---------
Public Property Get ErrorDesc
On Error Resume Next
If not isnumeric(p_Error) Then p_Error = 1
If p_Error=0 Then
ErrorDesc="没有错误!"
Else
ErrorDesc=p_ErrorDesc(p_Error)
If Err.Number > 0 Then
ErrorDesc=ErrorDesc & Err.Description
End If
End If
End Property
'================功能性属性=============================
'--------------属性: DbConnString DB连接字符串----------
Public Property Get DbConnString
DbConnString=p_DbConnString
End Property
Public Property Let DbConnString(strValue)
p_DbConnString=strValue
End Property
'==============此处以后是数据库关联的字段的 Get Let================
'--------------字段属性: Property1------------------
Public Property Get Property1
Property1=p_Property1
End Property
Public Property Let Property1(strValue)
p_Property1=strValue
End Property
'--------------字段属性:Property2------------------
Public Property Get Property2
Property2=p_Property2
End Property
Public Property Let Property2(strValue)
p_Property2=strValue
End Property
'--------------字段属性:Property3------------------
Public Property Get Property3
Property3=p_Property3
End Property
Public Property Let Property3(strValue)
p_Property3=strValue
End Property
'--------------字段属性:Property4------------------
Public Property Get Property4
Property4=p_Property4
End Property
Public Property Let Property4(strValue)
p_Property4=strValue
End Property
'--------------字段属性:Property5------------------
Public Property Get Property5
Property5=p_Property5
End Property
Public Property Let Property5(strValue)
p_Property5=strValue
End Property
'--------------字段属性:Property6------------------
Public Property Get Property6
Property6=p_Property6
End Property
Public Property Let Property6(strValue)
p_Property6=strValue
End Property
'--------------字段属性:Property7------------------
Public Property Get Property7
Property7=p_Property7
End Property
Public Property Let Property7(strValue)
p_Property7=strValue
End Property
'--------------字段属性:Property8------------------
Public Property Get Property8
Property8=p_Property8
End Property
Public Property Let Property8(strValue)
p_Property8=strValue
End Property
'--------------字段属性:Property9------------------
Public Property Get Property9
Property9=p_Property9
End Property
Public Property Let Property9(strValue)
p_Property9=strValue
End Property
'=================类过程和方法=================
'-----------打开数据库-----------
'如果已经存在连接,直接使用,否则创建连接
'
'参数:全局变量conn 和global变量dbConnString
'返回:连接是否正确 属性error 正确返回0,错误返回2
Public Sub DbOpen()
On Error Resume Next
set p_DbConnection=Server.CreateObject("ADODB.Connection")
If isobject(conn) Then '如果存在全局conn,使用已有的连接,退出并返回1
set p_DbConnection = conn
Exit Sub
End If
'否则创建并打开新的连结
p_DbConnection.open p_DbConnString
If err.number>0 Then
p_Error =2
End If
End Sub
'-----------关闭数据库-----------
'如果连接打开,关闭它
'
'参数:
'返回:连接是否关闭 属性error 正确关闭返回0,错误返回2
Public Sub DbClose()
On Error Resume Next
If isobject(p_DbConnection) Then '如果连接存在,关闭
p_DbConnection.close
set p_DbConnection = nothing
Exit Sub
End If
If err.number>0 Then
p_Error = 3
End If
End Sub
'-----------向数据库新增记录-----------
'用Add new语句插入记录 直接将 属性值 写入DB /需要打开数据库连接
' 此语法安全性较insert好,性能略差
'
'参数:private 变量
'返回:处理正确返回1,错误返回0
Public Sub AddNew()
On Error Resume Next
strSql="select * from " & p_TableName & " where 1=2;"
set rs=Server.CreateObject("ADODB.Recordset")
rs.open strSql,p_DbConnection,3,3
rs.Addnew
rs("Property1") = p_Property1
rs("Property2") = p_Property2
rs("Property3") = p_Property3
rs("Property4") = p_Property4
rs("Property5") = p_Property5
rs("Property6") = p_Property6
rs("Property7") = p_Property7
rs("Property8") = p_Property8
rs("Property9") = p_Property9
rs.Update
rs.close
set rs=nothing
If err.number>0 Then
p_Error=4
End If
End Sub
'-----------从数据库删除记录-----------
'用delete语句删除记录/需要打开数据库连接
'
'参数:private 变量
'返回:处理正确返回1,错误返回0
Public Function Delete()
On Error Resume Next
strSql="delete * from " & p_TableName & " Property1=" & p_Property1
p_DbConnection.execute strsql
If err.number>0 Then
p_Error = 6
End If
End Function
'-----------向数据库更新记录-----------
'用update语句更新记录/需要打开数据库连接
'
'参数:private 变量
'返回:处理 正确返回1,错误返回0
Public Function Update()
On Error Resume Next
strSql="select * from " & p_TableName & " Property1=" & p_Property1
set rs=Server.CreateObject("ADODB.Recordset")
rs.open strSql,p_DbConnection,3,3
If not (rs.bof and rs.eof) Then
If len(p_Property2)>0 Then
rs("Property2") = p_Property2
End If
If len(p_Property3)>0 Then
rs("Property3") = p_Property3
End If
If len(p_Property4)>0 Then
rs("Property4") = p_Property4
End If
If len(p_Property5)>0 Then
rs("Property5") = p_Property5
End If
If len(p_Property6)>0 Then
rs("Property6") = p_Property6
End If
If len(p_Property7)>0 Then
rs("Property7") = p_Property7
End If
If len(p_Property8)>0 Then
rs("Property8") = p_Property8
End If
If len(p_Property9)>0 Then
rs("Property9") = p_Property9
End If
rs.update
Else
p_Error = 9
End If
If err.number>0 Then
p_Error = 10
End If
rs.close
set rs=nothing
End Function
'-----------从数据库取一条新记录-----------
'用select语句取记录 /需要打开数据库连接
'
'参数:private 变量
'返回:处理 正确返回1,错误返回0
Public Function Fill()
On Error Resume Next
strSql="select * from " & p_TableName & " Property1=" & p_Property1
set rs=Server.CreateObject("ADODB.Recordset")
rs.open strSql,p_DbConnection,0,1
If not (rs.bof and rs.eof) Then
p_Property1 = rs("Property1")
p_Property2 = rs("Property2")
p_Property3 = rs("Property3")
p_Property4 = rs("Property4")
p_Property5 = rs("Property5")
p_Property6 = rs("Property6")
p_Property7 = rs("Property7")
p_Property8 = rs("Property8")
p_Property9 = rs("Property9")
Else
p_Error = 12
End If
If err.number>0 Then
p_Error = 11
End If
rs.close
set rs=nothing
End Function
'-----------从数据库取一条新记录-----------
'用select语句取记录 /需要打开数据库连接
'
'参数:private 变量
'返回:处理 正确返回1,错误返回0
Public Function List()
On Error Resume Next
strSql="select * from " & p_TableName & " Property1=" & p_Property1
set rs=Server.CreateObject("ADODB.Recordset")
rs.open strSql,p_DbConnection,0,1
If not (rs.bof and rs.eof) Then
p_Property1 = rs("Property1")
p_Property2 = rs("Property2")
p_Property3 = rs("Property3")
p_Property4 = rs("Property4")
p_Property5 = rs("Property5")
p_Property6 = rs("Property6")
p_Property7 = rs("Property7")
p_Property8 = rs("Property8")
p_Property9 = rs("Property9")
Else
p_Error = 12
End If
If err.number>0 Then
p_Error = 11
End If
rs.close
set rs=nothing
End Function
End Class
%>
<%
'-------------------------------------------------------------------------
'************************** 数据库-表-模版类 2.0 *************************
' 实现数据库中表TableName的 增 删 改 读单条记录 功能
'*************************************************************************
'私有变量:p_DbConnection:数据库连结,p_TableName:表名
'属性: DbConnString:数据库连结字符串
' Error:错误代码 ErrorDesc:错误信息 Version:版本信息
' Property1:字段1 Property2:字段2 Property3:字段3
' Property4:字段4 Property5:字段5 Property6:字段6
' Property7:字段7 Property8:字段8 Property9:字段9
'方法:
'DBOpen()
'DBClose()
'Insert() 直接将属性字段里面存储的信息用insert方法加入到数据库中
'AddNew() 直接将属性字段里面存储的信息用addnew方法加入到数据库中
'Delete() 用delete语句删除记录/需要打开数据库连接 参数:sql where 子句
'Update() 用update语句更新记录/需要打开数据库连接 参数:sql where 子句
'Fill() 用select语句取1条记录 /需要打开数据库连接 参数:sql where 子句
'
'使用方法:
'1 复制本模版到新文件,文件名按表名更改
'2 更改 有注释 [modify] 的部分
'3 更改 Property 为表中对应字段 (用replace,直接用字段名替换Property1,不要选择whole word only)
'4 调整各个字段的 get 和 let
'5 按需要处理错误代码
'
'---------------应用例子---------
'set tb1 = new TB_TableName ' 实例化
'tb1.DbOpen ' 打开db
'
'---addnew---
'tb1.Propeety 1="sdfsd" ' 向属性赋值
' . . .
'tb1.Propeety 9="sdfsd"
'tb1.addnew()
'
'---fill---
'tb1.fill() '
'
'---delete---
'tb1.delete() '
'
'---update---
'tb1.fill()
'tb1.Propeety1="sdfsd" 只update Propeety1的值
'tb1.update()
'----------------------------------------------------------
Class TB_TableName '[modify] 类名 更改为与表相关的名称
Private p_DbConnection,p_DbConnString '连接数据库变量
Private p_Rs,p_TableName '记录集,数据表名变量
Private p_Error,p_ErrorDesc(14) '错误信息
'-----下面的变量是数据表中的字段
Private p_Property1,p_Property2,p_Property3,p_Property4
Private p_Property5,p_Property6,p_Property7,p_Property8,p_Property9
'===============系统过程=========================
'--------------类初始化------------------
Private Sub Class_Initialize
On Error Resume Next
p_DbConnString = Application("DbConnString") 'Db连接字符串 [modify] 如果没有Application()变量直接输入连结字符串
p_TableName = "TableName" '[modify] 更改为数据表名称
p_Error = 0 '此值用于给用户返回错误信息 0 没错误 1-。。。下列对应错误
p_ErrorDesc(1) = "未知错误!"
p_ErrorDesc(2) = "数据库连结不成功!"
p_ErrorDesc(3) = "数据库关闭不成功!"
p_ErrorDesc(4) = "新增记录过程不成功!"
p_ErrorDesc(5) = "没有找到指定数据,无法更新数据!"
p_ErrorDesc(6) = "删除数据不成功!"
p_ErrorDesc(7) = ""
p_ErrorDesc(8) = "读取记录集不成功!"
p_ErrorDesc(9) = "没有找到要更新的记录!"
p_ErrorDesc(10) = "更新记录不成功!"
p_ErrorDesc(11) = "填充数据过程不成功!"
p_ErrorDesc(12) = "没有找到指定数据,无法填充数据!"
p_ErrorDesc(13) = ""
p_ErrorDesc(14) = ""
' p_Property = iniValue
End Sub
'--------------类销毁------------------
Private Sub Class_Terminate
On Error Resume Next
If isObject(p_DbConnection) Then '销毁数据库连结对象
p_DbConnection.close
set p_DbConnection = nothing
End If
If IsObject(p_Rs) Then '销毁Recordset对象
p_Rs.close
set p_Rs = nothing
End If
End Sub
'=============================
'----------属性: 返回版本号给用户-只读-------------------
Public Property Get Version
Version="DB Table Templete Class Version (1.0)"
Version=Version & "<BR>Made by Scott! Hbzhuxu@21cn.com"
End Property
'----------属性: 错误代码 返回错误代码给用户-只读--------
Public Property Get Error
Error=p_Error
End Property
'---------属性: 错误信息 返回错误说明给用户-只读---------
Public Property Get ErrorDesc
On Error Resume Next
If not isnumeric(p_Error) Then p_Error = 1
If p_Error=0 Then
ErrorDesc="没有错误!"
Else
ErrorDesc=p_ErrorDesc(p_Error)
If Err.Number > 0 Then
ErrorDesc=ErrorDesc & Err.Description
End If
End If
End Property
'================功能性属性=============================
'--------------属性: DbConnString DB连接字符串----------
Public Property Get DbConnString
DbConnString=p_DbConnString
End Property
Public Property Let DbConnString(strValue)
p_DbConnString=strValue
End Property
'==============此处以后是数据库关联的字段的 Get Let================
'--------------字段属性: Property1------------------
Public Property Get Property1
Property1=p_Property1
End Property
Public Property Let Property1(strValue)
p_Property1=strValue
End Property
'--------------字段属性:Property2------------------
Public Property Get Property2
Property2=p_Property2
End Property
Public Property Let Property2(strValue)
p_Property2=strValue
End Property
'--------------字段属性:Property3------------------
Public Property Get Property3
Property3=p_Property3
End Property
Public Property Let Property3(strValue)
p_Property3=strValue
End Property
'--------------字段属性:Property4------------------
Public Property Get Property4
Property4=p_Property4
End Property
Public Property Let Property4(strValue)
p_Property4=strValue
End Property
'--------------字段属性:Property5------------------
Public Property Get Property5
Property5=p_Property5
End Property
Public Property Let Property5(strValue)
p_Property5=strValue
End Property
'--------------字段属性:Property6------------------
Public Property Get Property6
Property6=p_Property6
End Property
Public Property Let Property6(strValue)
p_Property6=strValue
End Property
'--------------字段属性:Property7------------------
Public Property Get Property7
Property7=p_Property7
End Property
Public Property Let Property7(strValue)
p_Property7=strValue
End Property
'--------------字段属性:Property8------------------
Public Property Get Property8
Property8=p_Property8
End Property
Public Property Let Property8(strValue)
p_Property8=strValue
End Property
'--------------字段属性:Property9------------------
Public Property Get Property9
Property9=p_Property9
End Property
Public Property Let Property9(strValue)
p_Property9=strValue
End Property
'=================类过程和方法=================
'-----------打开数据库-----------
'如果已经存在连接,直接使用,否则创建连接
'
'参数:全局变量conn 和global变量dbConnString
'返回:连接是否正确 属性error 正确返回0,错误返回2
Public Sub DbOpen()
On Error Resume Next
set p_DbConnection=Server.CreateObject("ADODB.Connection")
If isobject(conn) Then '如果存在全局conn,使用已有的连接,退出并返回1
set p_DbConnection = conn
Exit Sub
End If
'否则创建并打开新的连结
p_DbConnection.open p_DbConnString
If err.number>0 Then
p_Error =2
End If
End Sub
'-----------关闭数据库-----------
'如果连接打开,关闭它
'
'参数:
'返回:连接是否关闭 属性error 正确关闭返回0,错误返回2
Public Sub DbClose()
On Error Resume Next
If isobject(p_DbConnection) Then '如果连接存在,关闭
p_DbConnection.close
set p_DbConnection = nothing
Exit Sub
End If
If err.number>0 Then
p_Error = 3
End If
End Sub
'-----------向数据库新增记录-----------
'用Add new语句插入记录 直接将 属性值 写入DB /需要打开数据库连接
' 此语法安全性较insert好,性能略差
'
'参数:private 变量
'返回:处理正确返回1,错误返回0
Public Sub AddNew()
On Error Resume Next
strSql="select * from " & p_TableName & " where 1=2;"
set rs=Server.CreateObject("ADODB.Recordset")
rs.open strSql,p_DbConnection,3,3
rs.Addnew
rs("Property1") = p_Property1
rs("Property2") = p_Property2
rs("Property3") = p_Property3
rs("Property4") = p_Property4
rs("Property5") = p_Property5
rs("Property6") = p_Property6
rs("Property7") = p_Property7
rs("Property8") = p_Property8
rs("Property9") = p_Property9
rs.Update
rs.close
set rs=nothing
If err.number>0 Then
p_Error=4
End If
End Sub
'-----------从数据库删除记录-----------
'用delete语句删除记录/需要打开数据库连接
'
'参数:private 变量
'返回:处理正确返回1,错误返回0
Public Function Delete()
On Error Resume Next
strSql="delete * from " & p_TableName & " Property1=" & p_Property1
p_DbConnection.execute strsql
If err.number>0 Then
p_Error = 6
End If
End Function
'-----------向数据库更新记录-----------
'用update语句更新记录/需要打开数据库连接
'
'参数:private 变量
'返回:处理 正确返回1,错误返回0
Public Function Update()
On Error Resume Next
strSql="select * from " & p_TableName & " Property1=" & p_Property1
set rs=Server.CreateObject("ADODB.Recordset")
rs.open strSql,p_DbConnection,3,3
If not (rs.bof and rs.eof) Then
If len(p_Property2)>0 Then
rs("Property2") = p_Property2
End If
If len(p_Property3)>0 Then
rs("Property3") = p_Property3
End If
If len(p_Property4)>0 Then
rs("Property4") = p_Property4
End If
If len(p_Property5)>0 Then
rs("Property5") = p_Property5
End If
If len(p_Property6)>0 Then
rs("Property6") = p_Property6
End If
If len(p_Property7)>0 Then
rs("Property7") = p_Property7
End If
If len(p_Property8)>0 Then
rs("Property8") = p_Property8
End If
If len(p_Property9)>0 Then
rs("Property9") = p_Property9
End If
rs.update
Else
p_Error = 9
End If
If err.number>0 Then
p_Error = 10
End If
rs.close
set rs=nothing
End Function
'-----------从数据库取一条新记录-----------
'用select语句取记录 /需要打开数据库连接
'
'参数:private 变量
'返回:处理 正确返回1,错误返回0
Public Function Fill()
On Error Resume Next
strSql="select * from " & p_TableName & " Property1=" & p_Property1
set rs=Server.CreateObject("ADODB.Recordset")
rs.open strSql,p_DbConnection,0,1
If not (rs.bof and rs.eof) Then
p_Property1 = rs("Property1")
p_Property2 = rs("Property2")
p_Property3 = rs("Property3")
p_Property4 = rs("Property4")
p_Property5 = rs("Property5")
p_Property6 = rs("Property6")
p_Property7 = rs("Property7")
p_Property8 = rs("Property8")
p_Property9 = rs("Property9")
Else
p_Error = 12
End If
If err.number>0 Then
p_Error = 11
End If
rs.close
set rs=nothing
End Function
'-----------从数据库取一条新记录-----------
'用select语句取记录 /需要打开数据库连接
'
'参数:private 变量
'返回:处理 正确返回1,错误返回0
Public Function List()
On Error Resume Next
strSql="select * from " & p_TableName & " Property1=" & p_Property1
set rs=Server.CreateObject("ADODB.Recordset")
rs.open strSql,p_DbConnection,0,1
If not (rs.bof and rs.eof) Then
p_Property1 = rs("Property1")
p_Property2 = rs("Property2")
p_Property3 = rs("Property3")
p_Property4 = rs("Property4")
p_Property5 = rs("Property5")
p_Property6 = rs("Property6")
p_Property7 = rs("Property7")
p_Property8 = rs("Property8")
p_Property9 = rs("Property9")
Else
p_Error = 12
End If
If err.number>0 Then
p_Error = 11
End If
rs.close
set rs=nothing
End Function
End Class
%>