Relative path reference in WebConfig.ConnectionString

后端 未结 5 1324
误落风尘
误落风尘 2020-12-15 23:12

Is it possible to specify a relative path reference in connectionstring, attachDbFileName property in a web.config?

For example, In my database is located in the App

5条回答
  •  爱一瞬间的悲伤
    2020-12-15 23:32

    It depends on where your '|DataDirectory|' is located. If the resolved value of '|DataDirectory|' is in folder A (where the web.config is), then no - you can't specify a relative path that is not a subfolder of the resolved value of '|DataDirectory|'.

    What you can do is set the value of '|DataDirectory|' to be wherever you would like, by calling the AppDomain.SetData method.

    From the MSDN online documentation:

    When DataDirectory is used, the resulting file path cannot be higher in the directory structure than the directory pointed to by the substitution string. For example, if the fully expanded DataDirectory is C:\AppDirectory\app_data, then the sample connection string shown above works because it is below c:\AppDirectory. However, attempting to specify DataDirectory as |DataDirectory|..\data will result in an error because \data is not a subdirectory of \AppDirectory.

    Hope this helps.

提交回复
热议问题