How to solve the relative path of the Access database in the Asp.net Program

Source: Internet
Author: User
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!

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.