| Datadirectory | solves the problem of the relative path of the webconfig database connection.

Source: Internet
Author: User
The database connection in webconfig does not support server. mappath. Generally, we can only write the physical path of the database file, which makes website migration very inconvenient. I found a solution on the internet today and shared it with you. See the following Code :

< Connectionstrings >
< Add Name = "Connstr_access"
Providername = "System. Data. oledb"
Connectionstring =
"Provider = Microsoft. Jet. oledb.4.0; Data Source = | datadirectory | \ cms. mdb ;"
/>
</ Connectionstrings >

| Datadirectory | it will be automatically parsed to app_data

Here is the description of msdn:

 

Replace string with datadirectory

AttachdbfilenameHas been extended in ADO. NET 2.0 and introduced| Datadirectory |Replacement string (included in the pipeline symbol.DatadirectoryAndAttachdbfilenameIn combination with the relative paths that can indicate data files, developers can create connection strings based on the relative paths of the database source (without specifying the full path.

DatadirectoryThe physical location of a vertex depends on the ApplicationProgram. In this example, the northwind. MDF file to be appended is located in the \ app_data folder of the application.

Data Source = . \ Sqlexpress; Integrated Security = True ;
User instance = True ;
Attachdbfilename = | Datadirectory | \ App_data \ northwind. MDF;
Initial catalog = Northwind;

UseDatadirectoryThe path of the result file in the directory structure cannot be higher than the directory pointed to by the replacement string. For exampleDatadirectoryIf it is c: \ appdirectory \ app_data, the connection string shown above is valid because it is under c: \ appdirectory. However, if you tryDatadirectorySpecified| Datadirectory | \ .. \ dataAn error occurs because \ data is not a subdirectory of \ appdirectory.

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.