Data | database | database connection
Careful developers sometimes think that when we include code like <!--#include file= "conn.asp" in a page that needs to read and write a database, in fact, when you do not have any read-write database operations, the database connection is still open, is still consuming the resources of the server.
So, is there any way we can get the database connection to open only when we need to read the database, without any action when we read it?
The idea is to encapsulate the database connection code in a function and call it when it needs to be read.
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, there was an error in the database connection. "
Response.End
End If
End Function
Call Method:
will be the original
Rs.Open Sql,conn
Change into
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& ";p rovider=microsoft. jet.oledb.4.0; "
Set Open_conn=conn
If ERR Then
Err. Clear
Conn.close:set conn=nothing
Response.Write "Sorry, there was an error in the database connection. "
Response.End
End If
End Function
Call Method:
will be the original
Rs.Open Sql,conn
Change into
Rs.Open Sql,open_conn ()
By the way, provide an old code that can share access databases across stations on the same server, perhaps with friends:
Sometimes, we have a lot of level two domain names, you may need to let these level two domain names to the same Access database, when you do not want to use the SQL database, the database connection can use the following methods. (Special note: If your server does FSO security permissions, you need to set this database directory to allow IIS users of the stations you need to call to access the modifications.) )
The following database physical path is similar to the e:\ directory \ Directory \ Database 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, there was an error in the database connection. "
Response.End
End If