ASP implementation makes database connections open only when you need to read the database

Source: Internet
Author: User
Tags include domain access database
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



Related Article

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.