The whole site only needs to use one database connection file to implement the function code. I won’t say anything else, just dedicate the original code directly.
Copy the code code as follows:
'==========Attention====================================== =========
' 1. The name of the folder where the website is located cannot be root.
' 2. The name of the folder where the website is located. Repeated folder names are not allowed in the physical path. For example, F:/t/t, t is the name of the folder where the website is located. In this case, an error will occur.
'================================================== ============
Dim dbPath,dbName,Db,ConnStr,WebSiteFolderName
on error resume next
WebSiteFolderName=WeldSiteBuilder
dbPath = _sitedata/ 'Note that it is a backslash, and only at the end
dbName = acteedata.mdb 'Database name
Db=GetSitePhysicalPath(WebSiteFolderName) & dbPath & dbName
'response.Write(current database physical path: &server.mappath(db))
'response.Write(<hr>)
'response.Write(Page path currently requested: &request.ServerVariables(URL))
'response.Write(<hr>)
'response.Write(root path of the current site: &request.ServerVariables(APPL_PHYSICAL_PATH))
'response.Write(<hr>)
'response.Write(path status of the 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
SetConn=Nothing
Response.Write database connection error!
Response.End
End If
SubCloseConn()
conn.close
set conn=nothing
end Sub
'Get the absolute path to the directory where the website is located (with / sign)
Function GetSitePhysicalPath(WSFN)
dim APPL_MD_PATH,APPL_MD_PATHArray,Path,WSPP
WSPP=request.ServerVariables(APPL_PHYSICAL_PATH)'The default website runs in 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 whether the website is running under an independent application
'Then determine whether the website is running in a certain folder
if InStr(WSPP,WSFN)=0 then 'The website runs in a folder in the root directory
'Spell out the address of the folder where the website is located based on the address of the visited page
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 website is running in the root directory
'No need to do anything, the return value has been initialized
end if
else 'indicates that the website is running in a virtual directory
'Do nothing, the return value has been initialized
end if
WSPP=replace(WSPP,//,/)
GetSitePhysicalPath=WSPP
End Function
PS: The above is just a solution to the problem of incorrect inclusion paths for the Access database, so that the site can share a database connection file. If any file contains it, the database can be opened normally, and there is no need to worry about how the website runs (run in the root directory, Run under the virtual path, run under the root directory + the directory where the website is located).