码迷,mamicode.com
首页 > 数据库 > 详细

关于数据库连接串中的DataDirectory的官方说明

时间:2018-03-31 18:18:54      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:spec   string   log   file   relative   als   oca   create   files   

Full path vs relative path

One of the reasons why it was hard to work with database files before is that the full path to the database was serialized in different places. This made it harder to share a project and also to deploy the application. In this version, the .NET runtime added support for what we call the DataDirectory macro. This allows Visual Studio to put a special variable in the connection string that will be expanded at run-time. So instead of having a connection string like this:

      "Data Source=.\SQLExpress;AttachDbFileName=c:\program files\app\data.mdf"

You can have a connection string like this:

      "Data Source=.\SQLExpress;AttachDbFileName=|DataDirectory|\data.mdf"

This connection string syntax is supported by the SqlClient and OleDb managed providers.

By default, the |DataDirectory| variable will be expanded as follow:

      - For applications placed in a directory on the user machine, this will be the app‘s (.exe) folder.
      - For apps running under ClickOnce, this will be a special data folder created by ClickOnce
      - For Web apps, this will be the App_Data folder

Under the hood, the value for |DataDirectory| simply comes from a property on the app domain. It is possible to change that value and override the default behavior by doing this:

      AppDomain.CurrentDomain.SetData("DataDirectory", newpath)

https://blogs.msdn.microsoft.com/smartclientdata/2005/08/26/working-with-local-databases/

关于数据库连接串中的DataDirectory的官方说明

标签:spec   string   log   file   relative   als   oca   create   files   

原文地址:https://www.cnblogs.com/woolhoo/p/8683094.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!