ASP contains path issues in files and solutions using a single database connection file _ Application Tips

Source: Internet
Author: User
Tags servervariables access database
Don't say anything, just give the original code
Copy Code code as follows:

' ========== Note ===============================================
' 1, the name of the folder in which the site is located cannot be "root"
' 2, the name of the folder where the site is located, the physical path is not allowed to duplicate this folder name, such as f:\t\t,t for the site folder name, this situation will be wrong
'=============================================================
Dim Dbpath,dbname,db,connstr,websitefoldername
On Error Resume Next

Websitefoldername= "Weldsitebuilder"
DBPath = "_sitedata\" ' notice is a backslash, and only the end has
dbname = "Acteedata.mdb" database name
Db=getsitephysicalpath (websitefoldername) & DBPath & dbname
' Response. Write ("Current Database physical path:" &server.mappath (db))
' Response. Write ("' Response. Write ("the page path for the current request access:" &request.) ServerVariables ("URL"))
' Response. Write ("' Response. Write ("Root Path of current site:" &request.) ServerVariables ("Appl_physical_path"))
' Response. Write ("' Response. Write ("Path Status of current site:" &request.) ServerVariables ("Appl_md_path"))
' Response. Write (DB)
' Response. End ()
'======================
Set conn=server.createobject ("ADODB. Connection ")
Connstr= "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" &db
Conn.Open ConnStr
If ERR Then
Err.Clear
Set Conn = Nothing
Response.Write "Database connection Error! "
Response.End
End If

Sub Closeconn ()
Conn.close
Set conn=nothing
End Sub

' Get the absolute path of the directory where the site is located (with the "\" number)
Function Getsitephysicalpath (WSFN)
Dim appl_md_path,appl_md_patharray,path,wspp
Wspp=request. ServerVariables ("Appl_physical_path") ' Default Web site is run under the root directory

Appl_md_path=request. ServerVariables ("Appl_md_path")
Appl_md_patharray=split (Appl_md_path, "/")
Path=appl_md_patharray (UBound (Appl_md_patharray))
If UCase (Path) = "ROOT" Then ' determine if the site is running under a stand-alone application
' And then judge if the site is running under a folder
If InStr (WSPP,WSFN) =0 then ' web site runs under a folder in the root directory
' The address of the folder where the site is located according to the address of the page being visited
Dim Currenturl,namearray
Currenturl=request. ServerVariables ("URL")
Namearray=split (Currenturl, "/")
For I=lbound (NameArray) to UBound (NameArray)
If UCase (NameArray (i)) =ucase (WSFN) Then
For j=0 to I
Wspp=wspp+namearray (j) + "\"
Next
Exit For
End If
Next
Else ' indicates that the site is running under the root directory
' Nothing to do, the return value has been initialized
End If
Else ' indicates that the site is running under a virtual directory
' Do nothing, the return value has been initialized
End If
Wspp=replace (WSPP, "\", "\")
Getsitephysicalpath=wspp
End Function

PS: The above is only for Access database to solve the inclusion of the wrong path, so that the site can be common to a database connection file, any file contains it, the database can be opened normally, and do not pay attention to how the site is running (root directory run, virtual path running, root directory + Run in the directory where the Web site resides).
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.