How to solve the relative path of the Access database in the Asp.net Program
Many of my friends are troubled by the relative path of the Access database in the. NET program, so every time a mobile program wants to modify web. config
Database path of the database connection string in.
The Web. config statement is as follows:
<Deleetask>
<Add key = "oledbconnectionstring" value = "provider = Microsoft. Jet. oledb.4.0; Data
Source = E:/web/app_data/data. mdb) "> </Add>
</Appsettings>
Write in the program as follows:
Myconn = new oledbconnection (system. configuration. configurationmanager. deleetpipeline ["oledbconnectionstring"]); // Note: The configurationsettings statements in vs2005 and vs2003 are different. Check the differences.
In this way, errors such as the following are often prompted during the program running:
'C:/Windows/system32 /~ /App_data/data. mdb 'is not a valid path. Check whether the path name is correctly spelled and whether it is connected to the server where the file is stored. Data Source = ~ /App_data/data. MDB
Even if the absolute path is correct, you need to modify web. config when porting the program, which is troublesome.
Some web. config also uses server. mappath like ASP to retrieve the database path. However, Web. config does not know server. mappath, and this method does not work.
Later, by exploring and referring to other programs, we summarized the following methods to facilitate program migration without having to modify the ACCESS database path.
My web. config statement is as follows:
<Deleetask>
<Add key = "sqlconnstring" value = "provider = Microsoft. Jet. oledb.4.0; Data Source ="/>
<Add key = "dbpath" value = "~ /App_data/mydata. mdb "/>
</Appsettings>
In the program's data category class, I took "sqlconnstring" and "dbpath" and connected them to a string "conn_string_non_dtc"
Public static readonly string conn_string_non_dtc = system. configuration. configurationmanager. appsettings ["sqlconnstring"]. tostring () + system. web. httpcontext. current. server. mappath (configurationmanager. appsettings ["dbpath"]) + ";";
This is the way in vs2005.
I defined conn_string_non_dtc as static readonly for ease of use.
Okay, so you can transplant your program without worrying about the database path. It's suitable for a lazy person like me!