Classes accessed by Access database

Source: Internet
Author: User
Tags access database

Most ASP applications, are inseparable from the access to the database and operations, so, for the database part of the access operation, we should be abstracted out, encapsulated into a separate class. If the language you are using supports inheritance, you can encapsulate one such class and then inherit it at the data manipulation layer. Here's an Access database Class I wrote, optimized for access, but because of the lack of adequate application testing, there may still be unknown bugs and application restrictions, the main code is as follows:

<%
Class OLE DB

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, adCmdText
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, adCmdStoredProc
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 With
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, adCmdStoredProc)
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 With
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/data.mdb" Here Write your database default path, suggest changing 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.