Page 1/3 of ACCESS class for an ACCESS database

Source: Internet
Author: User

Most ASP applications are inseparable from database access and operations. Therefore, we should separate the access operations on the database and encapsulate them into a separate class. If the language used supports inheritance, You can encapsulate such a class and inherit it at the data operation layer. The following is an ACCESS database ACCESS class I wrote, Which is optimized for ACCESS. However, due to the lack of sufficient application tests, there may still be unknown bugs and application restrictions. The main code is as follows:
<%
Class Oledb Private IDataPath
Private IConnectionString Private Conn
Private Cmd
Private Param
Private Rs Public Property Let DataPath (ByVal Value)
IDataPath = Value
IConnectionString = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" & Server. MapPath (IDataPath)
End Property Public Property Get DataPath ()
DataPath = IDataPath
End Property Public Property Let ConnectionString (ByVal Value)
IConnectionString = Value
End Property Public Property Get ConnectionString ()
ConnectionString = IConnectionString
End Property Public Function OpenConn ()
If Conn. State = adStateClosed Then
Conn. Open ConnectionString
End If
Set OpenConn = Conn
End Function Public Function Insert (ByVal SQL, ByVal Values)
OpenConn ()
Rs. Open SQL, Conn, 3, 3, ad1_text
Rs. AddNew
Dim I, l
L = UBound (Values)
For I = 1 To l + 1
Rs (I) = Values (I-1)
Next
Rs. Update
Insert = Rs (0)
End Function Public Function Execute (ByVal SQL)
OpenConn ()
Set Execute = Conn. Execute (SQL)
End Function Public Function ExecuteScalar (ByVal SQL)
Dim iRs: Set iRs = Execute (SQL)
If Not iRs. BOF Then ExecuteScalar = iRs (0)
End Function Public Function ExecuteNonQuery (ByVal SQL)
OpenConn ()
Call Conn. Execute (SQL, ExecuteNonQuery)
End Function Public Function InsertSp (ByVal SQL, ByVal Params)
OpenConn ()
Rs. Open SQL, Conn, 3, 3, ad1_storedproc
Rs. AddNew
Dim I, l
L = UBound (Params)
For I = 1 To l + 1
Rs (I) = Params (I-1)
Next
Rs. Update
InsertSp = Rs (0)
End Function Public Function ExecuteSp (ByVal SpName, ByVal Params)
With Cmd
Set. ActiveConnection = OpenConn ()
. CommandText = SpName
. CommandType = & H0004
. Prepared = True
Set ExecuteSp =. Execute (, Params)
End
End Function Public Function ExecuteDataTableSp (ByVal SpName, ByVal Params)
OpenConn ()
If Rs. State <> adStateClose Then
Rs. Close ()
End If
Dim SpStr
If IsNull (Params) Or IsEmpty (Params) Then
SpStr = SpName
Else
If IsArray (Params) Then
SpStr = "Execute" & SpName & "& Join (Params ,",")
Else
SpStr = "Execute" & SpName & "& Params
End If
End If
Call Rs. Open (SpStr, Conn, 1, 1, ad1_storedproc)
Set ExecuteDataTableSp = Rs
End Function Public Function ExecuteScalarSp (ByVal SpName, ByVal Params)
Dim iRs: Set iRs = ExecuteSp (SpName, Params)
If Not iRs. BOF Then ExecuteScalarSp = iRs (0)
End Function Public Function ExecuteNonQuerySp (ByVal SpName, ByVal Params)
With Cmd
Set. ActiveConnection = OpenConn ()
. CommandText = SpName
. CommandType = & H0004
. Prepared = True
Call. Execute (ExecuteNonQuerySp, Params)
End
End Function Private Sub Class_Initialize ()
Set Conn = Server. CreateObject ("ADODB. Connection ")
Set Cmd = Server. CreateObject ("ADODB. Command ")
Set Param = Server. CreateObject ("ADODB. Parameter ")
Set Rs = Server. CreateObject ("ADODB. RecordSet ")
DataPath = "/data. mdb" 'enter the default path of your database. We recommend that you change the name and extension.
End Sub
Private Sub Class_Terminate ()
Set Param = Nothing
Set Cmd = Nothing
CloseRs ()
CloseConn ()
End Sub Private Sub CloseConn ()
If Conn. State <> adStateClose Then
Conn. Close ()
Set Conn = Nothing
End If
End Sub Private Sub CloseRs ()
If Rs. State <> adStateClose Then
Rs. Close ()
Set Rs = Nothing
End If
End Sub End Class
%>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.