前幾天做了個asp實用類庫.
--AspLib '放在根目錄
|--System
|--adovbs.inc
|--Util
|--Configuration.asp
|--DataAccess.asp
|--Validator.asp
|--......
|--Control
|--DropDownList.asp
|--DataList.asp
|--DataGrid.asp
|--Script
|--DataGrid.js
|--Images
|--DataGrid
|--First.gif
|--Next.gif
|--....
首先先看一下資料庫操作類(DataAccess):
<!--#Include File="../System/adovbs.inc"-->
<%
'<class>
'<name>DataAccess</name>
'<description><![CDATA[資料庫訪問類(2.0版);ExecuteReader方法返回斷開的記錄集對象,而不是自訂的DataTable對象.每個方法都保證最
少消耗資料庫資源。資料庫連接對象使用用戶端資料指標,使記錄集可使用RecordCount等屬性。增加debug功能。和類configuration組合,減少
strConnectionString參數(以前方法為ExecuteReader(strConn,strCommadnText)),可是增加了耦合度!作者:聖誕菠蘿包]]></description>
'<attributes>
'</attributes>
'<methods>
' <method name="ExecuteReader(ByVal strCommandText)" return="Recordset" comment="執行資料庫查詢操作,返回斷開的記錄集對
象"/>
' <method name="ExecuteNonQuery(ByVal strCommandText)" return="boolean" comment="執行資料庫更新/刪除操作,返回受影響行數
"/>
' <method name="ExecuteScalar(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_
'連接字串
Private connectionString_
'Debug
Private debug_
'執行資料庫查詢操作,返回斷開的記錄集對象
Public Function ExecuteReader(ByVal strCommandText)
If debug_=False Then
On Error Resume Next
End If
'開啟資料庫連接
Call OpenConnection()
'設定command
Call SetCommand(strCommandText)
Dim objRS,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 strCommandText)
If debug_=False Then
On Error Resume Next
End If
'開啟資料庫連接
Call OpenConnection()
'設定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 strCommandText)
If debug_=False Then
On Error Resume Next
End If
'開啟資料庫連接
Call OpenConnection()
'設定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)
If debug_=False Then
On Error Resume Next
End If
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)
If debug_=False Then
On Error Resume Next
End If
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()
If debug_=False Then
On Error Resume Next
End If
If Not IsNull(connection_) Then
Set connection_=Server.CreateObject("ADODB.Connection")
connection_.CursorLocation=adUseClient
connection_.Open(connectionString_)
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
connectionString_=new Configuration.GetAppSetting("ConnectionString")
Set command_=Server.CreateObject("ADODB.Command")
If new Configuration.GetSystemSetting("Debug")="True" Then
debug_=True
Else
debug_=False
End If
End Sub
'銷毀類
Private Sub Class_Terminate()
Call CloseCommand()
Call CloseConnection()
End Sub
End Class
%>
在前一個版本,方法都帶有strConnectionString參數,如ExecuteReader(strConnectionString,strCommandText);
我還是比較推薦前一個版本的DataAccess.(因為copy就可以,不用理設定檔)
但我是個懶人,所以增加了個設定檔(Asp_Web.Config),減少strConnectionString參數,同時還增加了Debug功能;
Asp_Web.Config如下:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="ConnectionString" value="Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Pwd=XXXX;Initial Catalog=XXXXX;Data Source=localhost"/>
<add key="Admin_Ticket" value="CHCW_ADMIN"/>
<add key="UploadImage_MaxSize" value="102400"/>
</appSettings>
<system>
<Debug value="True"/>
</system>
</configuration>
在實際測試中,用ExecuteReader方法取資料比Rs.Open sql,conn取資料慢一點;但比較大的減少資料庫的資源消耗;因為ExecuteReader返回的是一個斷開的記錄集;
好,看看例子:
'先包含
<!--#Include Virtual="/AspLib/Util/Configuration.asp"-->
<!--#Include Virtual="/AspLib/Util/DataAccess.asp"-->
---ExecuteReader方法---
<%
Dim objRS
Set objRS=new DataAccess.ExecuteReader("Select Top 20 NewsID,NewsTitle,UpdateTime From News Where=" & NewsCategoryID)
While Not objRS.EOF
Response.Write(objRS(0) & objRS(2) & "<br />")
objRS.MoveNext
Wend
objRS.Close()
Set objRS=Nothing
%>
<%
'預存程序
Dim objRS,objDBA
Set objDBA=new DataAccess
objDBA.CreateParameter "@NewsCategoryID",adInteger,adParamInput,4,1
Set objRS=new DataAccess.ExecuteReader("GetNewsByCategoryID")
While Not objRS.EOF
Response.Write(objRS(0) & objRS(2) & "<br />")
objRS.MoveNext
Wend
objRS.Close()
Set objRS=Nothing
%>
----ExecuteNonQuery方法-----
<%
Dim intRecordsAffected
intRecordsAffected=new DataAccess.ExecuteNonQuery("Update News Set NewsTitle='新標題' Where NewsID=10")
Response.Write("更新行數:" & intRecordsAffected)
%>
<%
'預存程序
Dim objDBA,intRecordsAffected
Set objDBA=new DataAccess
objDBA.CreateParameter "@intNewsID",adInteger,adParamInput,4,10
objDBA.CreateParameter "@strNewsTitle",adVarchar,adParamInput,50,"新標題"
intRecordsAffected=objDBA.ExecuteNonQuery("News_UpdateTitle")
If intRecordsAffected=0 Then
Response.Write("更新失敗!")
Else
Response.Write("更新成功!")
End If
Set objDBA=Nothing
%>
-----ExecuteScalar方法----
<%
Dim intReturn
intReturn=new DataAccess.ExecuteScalar("Select Count(*) From News")
Response.Write("總記錄數:" & intReturn)
%>