JS these days began to contact JSP inside some of the bean's writing, and then think about it, that in fact, in the ASP can also take this idea to do. Although not very pure, not thorough, but can be some logical processing separation, more suitable for the transplant of the program, improve the development cycle. I wrote a class myself. Connex contains some operations on the database, think should be able to include a large part of the logical processing, but this also increases the error probability, if you control the SQL statement better, it should be more advantages than disadvantages, here are a little humble opinion, I hope you correct me.
program features a general framework, in fact, you can add some features, such as the beginning of the database connection, you can set the variable and then through Init () to select a different type of database
<%
'*******************************************************************************************
' * Program: connex.asp
'*
' * Description: Imitate JavaBean write a class, specialized operation database, provide a variety of methods to operate, but to avoid SQL syntax errors!
'*
' * Author: field email:foxty@sina.com
'*
' * Date: 2005.06.0
'*******************************************************************************************
' on Error Resume Next
Class Connex
Public Connex
public dbpath '---------database path
public DBtype '---------database type 1 (Access) 2 (SQL Server) 3 (expandable)
public connmethod '--------connection mode (DSN, non-DSN)
Public User
Public Pass
Sub Class_Initialize
End Sub
Sub Init ()
connstr = "Driver={microsoft Access Driver (*.mdb)};d bq=" &server.mappath ("Date.mdb")
Set Connex = Server.CreateObject ("ADODB. CONNECTION ")
Connex.open connstr
catcherror ("Class_Terminate")
End Sub
Sub catcherror (STR)
If ERR Then
Err.Clear
Class_Terminate ()
Response.Write ("Catch error, end of program!" at "&Str&")
Response.End ()
End If
End Sub
'******************************************
' * Through the SQL statement to find the existence of records, error prone
'******************************************
Function Hasrecordbysql (SQL)
Call Checksql (SQL, "R")
Dim Rs,hasr
Set Rs = Connex.execute (SQL)
catcherror ("Hasreordsql")
If not (rs.eof Or rs.bof) Then
Hasr = False
Else
Hasr = True
End If
Rs.close
Set Rs = Nothing
Hasrecordbysql = Hasr
End Function
'***************************************
' * To find out if a record exists by ID
'***************************************
Function Hasrecordbyid (strtablename, IntID)
' CheckValue (IntID, 1)
Dim Rs,hasr
Sql = "SELECT top 1 * from" &StrTableName& "Where Id =" &intid
Call Checksql (SQL, "R")
Set Rs = Connex.execute (SQL)
catcherror ("Hasrecordbyid")
If not (rs.eof Or rs.bof) Then
HASR = False
Else
Hasr = True
End If
Rs.close
Set Rs = Nothing
Hasrecordbyid = Hasr
End Function
'**********************************************
' * Get recordset from SQL statement
'**********************************************
Function getrsbysql (SQL)
Call Checksql (SQL, "R")
Dim Rs
Set Rs = Server.CreateObject ("Adodb.recordset")
Rs.Open sql,connex,1,1
Set getrsbysql = Rs
End Function
'*********************************************
' * Gets the value of a field
'*********************************************
Function getvaluebysql (SQL)
Call Checksql (SQL, "R")
Dim Rs,returnvalue
Set Rs = Connex.execute (SQL)
Catcherror ("Getvaluebysql")
If not (rs.eof Or rs.bof) Then
returnvalue = Rs (0)
Else
returnvalue = "no Record"
End If
Rs.close
Set Rs = Nothing
Getvaluebysql = returnvalue
End Function
' ==================================================update,insert========================================== ========================
'*********************************************
' * Using SQL to modify data
'*********************************************
Function updatebysql (SQL)
Call Checksql (SQL, "w")
Connex.execute (SQL)
catcherror ("Updatebysql")
Updatebysql = True
End Function
'********************************************
' * INSERT data using SQL statements
'********************************************
Function insertbysql (SQL)
Call Checksql (SQL, "w")
Connex.execute (SQL)
catcherror ("Insertbysql")
Insertbysql = True
End Function
' ======================================================delete============================================== ===============
'*******