<!--#Include File="../System/adovbs.inc"-->
<%
'<class>
'<name>DataAccess</name>
'<description><![CDATA[数据库访问类。作者:圣诞菠萝包]]></description>
'<attributes>
'</attributes>
'<methods>
' <method name="ExecuteReader(ByVaL strConnString,ByVal strCommandText)" return="Recordset" comment="执行数据库查询操作,返回断开的记录集对象"/>
' <method name="ExecuteNonQuery(ByVal strConnString,ByVal strCommandText)" return="boolean" comment="执行数据库更新/删除操作,返回受影响行数"/>
' <method name="ExecuteScalar(ByVal strConnString,ByVal strCommandText)" return="variant" comment="执行查询,并返回查询所返回的结果集中第一行的第一列。忽略额外的列或行;"/>
' <method name="CreateParameter(ByVal strName,ByVal intDataType,ByVal intDirection,ByVal intSize,ByVal varValue)" comment="向类内的参数集合添加参数对象"/>
' <method name="GetParameterValue(ByVal strParamName)" return="variant" comment="返回指定参数名的值"/>
' <method name="ClearParameter()" return="variant" comment="清空参数集合"/>
'</methods>
'</class>
Class DataAccess
'数据库连接对象
Private connection_
'数据库命令对象
Private command_
'执行数据库查询操作,返回断开的记录集对象
Public Function ExecuteReader(ByVaL strConnString,ByVal strCommandText)
On Error Resume Next
'打开数据库连接
Call OpenConnection(strConnString)
'设置command
Call SetCommand(strCommandText)
Dim objRS,arrCol,arrRow,i,intCol,objOutRS
Set objRS=command_.Execute()
Set objOutRS=Server.CreateObject("ADODB.RecordSet")
objOutRS.Open objRS
'返回断开的记录集
Set ExecuteReader=objOutRS
If Err.Number<>0 Then
Err.Clear()
Response.Write("数据库读取操作错误")
Response.End()
End If
objRS.Close()
Set objRS=Nothing
Call CloseConnection()
End Function
'数据库更新,删除操作(存储过程)
Public Function ExecuteNonQuery(ByVal strConnString,ByVal strCommandText)
On Error Resume Next
'打开数据库连接
Call OpenConnection(strConnString)
'设置command
Call SetCommand(strCommandText)
Dim intRecordsAffected
command_.Execute intRecordsAffected, , adExecuteNoRecords
ExecuteNonQuery=intRecordsAffected
If Err.Number<>0 Then
Err.Clear()
Response.Write("数据库更新操作错误")
Response.End()
End If
Call CloseConnection()
End Function
'执行查询,并返回查询所返回的结果集中第一行的第一列。忽略额外的列或行
Public Function ExecuteScalar(ByVal strConnString,ByVal strCommandText)
On Error Resume Next
'打开数据库连接
Call OpenConnection(strConnString)
'设置command
Call SetCommand(strCommandText)
Dim objRS
Set objRS=command_.Execute()
If Not objRS.EOF Then
ExecuteScalar=objRS(0)
End If
If Err.Number<>0 Then
Err.Clear()
Response.Write("数据库读取单值操作错误")
Response.End()
End If
objRs.Close()
Set objRS=Nothing
Call CloseConnection()
End Function
'创建参数
Public Function CreateParameter(ByVal strName,ByVal intDataType,ByVal intDirection,ByVal intSize,ByVal varValue)
On Error Resume Next
Dim objParam
If intDirection=4 Then
Set objParam=command_.CreateParameter(strName,intDataType,intDirection)
ElseIF intDirection=2 Then
Set objParam=command_.CreateParameter(strName,intDataType,intDirection,intSize)
Else
Set objParam=command_.CreateParameter(strName,intDataType,intDirection,intSize,varValue)
End If
command_.Parameters.Append(objParam)
If Err.Number<>0 Then
Err.Clear()
Response.Write("创建参数失败")
Response.End()
End If
End Function
'取得指定参数名的参数值
Public Function GetParameterValue(ByVal strParamName)
On Error Resume Next
GetParameterValue=command_.Parameters(strParamName).Value
If Err.Number<>0 Then
Err.Clear()
GetParameterValue=""
End If
End Function
'清除参数
Public Function ClearParameter()
Dim i
For i=0 To command_.Parameters.Count - 1
command_.Parameters.Delete(0)
Next
End Function
'初始化数据库连接对象
Private Sub OpenConnection(ByVal strConnString)
On Error Resume Next
If Not IsNull(connection_) Then
Set connection_=Server.CreateObject("ADODB.Connection")
connection_.CursorLocation=adUseClient
connection_.Open(strConnString)
If Err.Number<>0 Then
Err.Clear()
CloseConnection(connection_)
Response.Write("数据库连接错误")
Response.End()
End If
End If
End Sub
'关闭数据库连接对象
Private Sub CloseConnection()
If Not connection_ Is Nothing Then
connection_.Close()
Set connection_=Nothing
End If
End Sub
'初始化数据库命令对象
Private Sub SetCommand(ByVal strCommandText)
command_.ActiveConnection=connection_
If command_.parameters.Count>0 Then
command_.CommandType=4
Else
command_.CommandType=1
End If
command_.Prepared=True
command_.CommandText=strCommandText
End Sub
'关闭数据库命令对象
Private Sub CloseCommand()
If Not command_ Is Nothing Then
Set command_=Nothing
End If
End Sub
'初始化类
Private Sub Class_Initialize()
On Error Resume Next
Set command_=Server.CreateObject("ADODB.Command")
End Sub
'销毁类
Private Sub Class_Terminate()
Call CloseCommand()
Call CloseConnection()
End Sub
End Class
%>