Path issues for Access databases in ASP

Source: Internet
Author: User
Tags include odbc servervariables access database root directory
access| Data | database | Problems you usually do most of the site using SQL database, so there is no contact with Access database sites. Yesterday, when I was helping my friends with a Web site Admin program using an Access database, I got into some trouble.



The directory settings for this site are as follows (only the relevant sections are listed)


F1 directory is the background management program, F2 directory of Mydata.mdb is a database file, cnn.asp the database to specify the way, in other files in the way to include files.



Because Mydata.mdb and cnn.asp are in the same directory, the connection statement in Cnn.asp is written like this:

STRCNN = "Driver={microsoft Access Driver (*.mdb)};d bq=" & _

Server.MapPath ("Mydata.mdb")



Because it was originally written, I did not think carefully about what, directly in the F1 directory of the index.asp file to add the F2 statement

<!--#include file= ". /f2/cnn.asp "-->

Then run ... Well??? There's been a mistake!!

Microsoft OLE DB Provider for ODBC Drivers (0x80004005)
[Microsoft] [ODBC Microsoft Access Driver] Common error cannot open registry key ' temporary (volatile) Jet DSN for process 0x94 Thread 0x9a0 DBC 0x13b0074 T '.

The error prompted an error in the open database and the database was not found. is the database path wrong?



Look at the strcnn output in the cnn.asp first

Driver={microsoft Access Driver (*.mdb)};d Bq=c:\f1\mydata.mdb

Sure enough, the path is wrong!



It seems that this is the Server.MapPath the path to the page file containing the cnn.asp file as the current path. That is, if the index.asp in the F1 directory contains cnn.asp, then Server.MapPath ("Mydata.mdb") is C:\F1\ Mydata.mdb if a file under the F1 directory contains a cnn.asp, then Server.MapPath ("Mydata.mdb") is C:\F1\News\mydata.mdb;

Verified that the situation is indeed the case.



If the problem is found, it should be solved. Because you cannot qualify to include cnn.asp files in a few levels, you cannot get the correct path to the database using Server.MapPath; Do you want to write all the connections to the database in all the files that use the database? This is the next worst, try to avoid.



After n minutes of thinking, finally came up with a more cost-effective way is to not use the virtual path, first determine the actual path of the root directory, and then join the database path. The implementation code is as follows:



cnn.asp file:



Dim strcurpath,strcurlocation



' Get the virtual path of the paging file that contains the file

Strcurpath=request.servervariables ("Path_info")

' Get the actual path of the paging file that contains the file

Strcurlocation=request.servervariables ("path_translated")



' Convert path interval symbol (virtual path separated by '/', actual path separated by ' \ ')

Strcurpath=replace (Strcurpath, "/", "\")



' Get the actual path to the site root directory

Strcurlocation=replace (Strcurlocation,strcurpath, "")



' Specify the actual path of the database

Strcurlocation=strcurlocation & "\f2\mydata.mdb"



Set Cnn = Server.CreateObject ("ADODB.") CONNECTION ")

' Connect to database server, database name Mydata.mdb

STRCNN = "Driver={microsoft Access Driver (*.mdb)};d bq=" & strcurlocation

Cnn.open strcnn



That's it, throw the bricks.





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.