Implementation of universal absolute path generation method in ASP

Source: Internet
Author: User
When I develop the ASP + Access Web application, I will encounter the problem of locating the path of the database file. When implemented on your own computer, you can specify the event directly. But if you do the project to upload to other servers or mobile other machines to run the use, each time to change the path to change the words appear to be some fan lock, the following combined with my experience to do this site to solve this problem, so that we can "once and for all".

When the Server.MapPath ("file.asp") statement is used in ASP, it returns the absolute path of the file on the local drive, such as "D:\MySite\test\file.asp", So using this statement in different subdirectories will only return the path where the file is located, so we can't simply use this statement to solve the problem above, but it's easy to solve the problem with a little bit of this method and other character functions.

Now let's assume that your site directory structure is as follows:
MySite
|-DB
|-Db1.mdb
|-Db2.mdb
|-Sub_folder
|-hello.asp
|-search.asp
|-index.asp

As long as we add the following statement to the ASP file that requires access to the database:


If session ("RootDir") = "" Then
Dim str
str = Server.MapPath ("db")
Session ("RootDir") = Mid (str, 1, INSTR (1, str, "MySite", 1)-1) & "Mysite\db"
End If
...
Set conn = Server.CreateObject ("Adodb.connection")
Conn. Open "Driver={microsoft Access driver (*.mdb)};d bq=" & Session ("RootDir") & "\db1.mdb" & ";p wd=" & PW


So in the session ("RootDir") stored in your site Neutron directory "db" of the knot path, when you change the home directory of your site or move to another machine in case you can not ignore the database file access path problem.



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.