Environment variables in ASP.NET web.config file

I was wondering why I cannot use a custom environment variable in an ASP.NET web.config file, for example?

<?xml version="1.0"?> <configuration> <connectionStrings> <add name="ConnectionName" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename='%MyProjectsFolder%\WebAppName\App_Data\Database1.mdf';User Instance=true" providerName="System.Data.SqlClient" /> </connectionStrings> </configuration> 

I checked through Start → Run that the path to the .mdf file is valid.

When I run my C # code to connect to the database, I get the following error:

An attempt to mount a database with auto-name for the file% MyProjectsFolder% \ WebAppName \ App_Data \ Database1.mdf failed. a database with the same name exists, or the specified file cannot be opened or is located on the UNC shared folder.

+4
source share
3 answers

Environment variables cannot be used in the configuration file.

+5
source

You can use: DataDirectory as follows:

 "Data Source = |DataDirectory|\Mydb.sdf" 

And you can change where the DataDirectory indicates: "To set the DataDirectory property, call AppDomain.SetData"

http://social.msdn.microsoft.com/Forums/en/sqlce/thread/dc31ea59-5718-49b6-9f1f-7039da425296

Caution, I have never tried this.

+1
source

I do not think you have tried:

 connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename='~/App_Data/Database1.mdf';User Instance=true" 
0
source

Source: https://habr.com/ru/post/1397707/


All Articles