ASP. NET connection to access database relative path problem how to use relative path in Web. config and call this relative path in class files

Source: Internet
Author: User

The relative path of ASP. NET to access the database. How to use the relative path in Web. config and call this relative path in the class file (Source code)?

I am using VS 2005. After debugging, how can I implement it,CodeAs follows:

 

First, add the following code to the <etettings> node of the web. config file:
<! -- Data source -->
<Add key = "myds" value = "provider = Microsoft. Jet. oledb.4.0; Data Source ="/>
<! -- Database relative path -->
<Add key = "myconn" value = "app_data \ database. mdb"/>

 

Then, write a class file and call the above definition. The Code is as follows:

'The difficulty lies in defining a connection string. Many servers are used online. mappath, but it still does not work. Note that the request is used here. mappath (this is useless to ASP)
Public connstr as string = configurationsettings. receivettings ("myds") + httpcontext. Current. Request. mappath ("~ ") + (Configurationsettings. receivettings (" myconn "). Trim ())

 

There is also a better method: this is a new feature of Asp.net 2.0, using | datadirectory |
In the Web. config file:
<Add name = "connstr" connectionstring = "provider = Microsoft. jet. oledb.4.0; Data Source = | datadirectory | \ dB. mdb "providername =" system. data. oledb "/>

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/yydy1983/archive/2007/07/19/1698536.aspx

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.