ASP. NET Connect Access database relative path notation

Source: Internet
Author: User
Tags sql server express connectionstrings
When ASP. NET connects to an Access database, we typically write the database connection string to the Web. config configuration file. The database paths in the connection string can only be represented in absolute paths, so if you want to move the program, you have to modify the database path of the database connection strings in Web. config, which is cumbersome. If written in relative path form such as: ~/database/test.mdb is also incorrect. For example:

<connectionstrings>    <add name= "Access" connectionstring= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=~/database/test.mdb "providername=" System.Data.OleDb "/>  </connectionstrings>

The above notation is wrong.

There are currently 2 common solutions:

1, by using the DataDirectory keyword method

Starting with ASP. NET 2.0, there is a App_Data directory dedicated to storing data files that can be used to Access,sql Server Express, XML, and other data files. You can put the Access database file in the App_Data folder and use the keyword Datadirectoty to get the path.

<connectionstrings>    <add name= "Access" connectionstring= "Provider=Microsoft.Jet.OLEDB.4.0;Data source=| Datadirectory|test.mdb "  providername=" System.Data.OleDb "/>  </connectionstrings>

2. Set two strings in the Web. config file

Set two strings in the Web. config file, one for the drive string and the other for the relative path to the Access database file. Use Server.MapPath () to get the absolute path, and then the combined connection string can be used.

<connectionStrings>    <add name= "Access" connectionstring= "Provider=Microsoft.Jet.OLEDB.4.0;Data source={0} "      providername=" System.Data.OleDb "/></connectionstrings><appsettings>    <add key= "Accesspath" value= "~/database/test.mdb"/></appsettings>

The following code is used in the background:

private String Getconnstr () {    string connstr = webconfigurationmanager.connectionstrings["Access"]. ConnectionString;    ConnStr = Connstr.replace ("{0}", Server.MapPath (webconfigurationmanager.appsettings["Accesspath"]. ToString ()));    return connstr;}
  • 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.