Careful developers sometimes think that when we include code like
<!-- #include file="conn.asp" -->
in a page that needs to read and write to the database, in fact, when you do not perform any reading or writing During database operations, the database connection is still open and is still consuming server resources.
So, is there any way for us to make the database connection open only when the database needs to be read, and there will be no action when it is not read? This idea is provided below to inspire others.
This idea is to encapsulate the database connection code in a function and call this function when reading is needed.
The following is the SQL connection code:
Function Open_conn()
dim Conn,Strconn
set Conn=server.createobject("adodb.connection")
Strconn = "Provider = Sqloledb; User ID = database login account; Password = database login password; Initial Catalog = database name; Data Source = (local);"
Conn.open Strconn
set Open_conn=Conn
If Err Then
err.Clear
Conn.close:set Conn=nothing
Response.Write "Sorry, database connection error."
Response.End
End If
End Function
calling method:
Change the original
rs.open sql, conn
to
rs.open sql, Open_conn().
The following is the ACCESS connection code:
Function Open_conn()
dim Dbpath,Conn
Dbpath=server.MapPath("database path")
set Conn=server.createObject("ADODB.connection")
Conn.open "data source="&dbpath&";provider=microsoft.Jet.OLEDB.4.0;"
set Open_conn=Conn
If Err Then
err.Clear
Conn.close:set Conn=nothing
Response.Write "Sorry, database connection error."
Response.End
End If
End Function
calling method:
replace the original
rs.open sql, conn
changed to
rs.open sql, Open_conn()
by the way provides an old code that can share the ACCESS database across sites on the same server. Maybe some friends can use it:
sometimes, we have many second-level domain names, and we may need to let these second-level domain names call a certain An ACCESS database, if you are unwilling to use a SQL database, the following methods can be used for database connection. (Special note: If your server has FSO security permissions, you need to set this database directory to allow the IIS users of each station you need to call to have access and modification.)
The following physical path to the database is similar to E:Directory Directorydatabase name
dim Conn, Strconn
Set Conn = Server.CreateObject("ADODB.Connection")
Strconn="Provider = Microsoft.Jet.OLEDB.4.0; "
Strconn=Strconn & "Data Source=Database physical path"
Conn.Open Strconn
If Err Then
err.Clear
Conn.close:set Conn=nothing
Response.Write "Sorry, database connection error."
Response.End
End If