ADO.NET |DataDirectory| where is this documented?

人走茶凉 提交于 2019-12-16 20:07:25

问题


In AppConfig it is possible to use |DataDirectory| but I can't find any doc ?


回答1:


|DataDirectory| is a substitution string so you can configure the location of your database file separately.

So instead of:

SqlConnection c = new SqlConnection (
   @"Data Source=.\SQLDB; AttachDbFilename=C:\MyDB\Database.mdf;Initial Catalog=Master");

you do the following:

// Set |DataDirectory| value
AppDomain.CurrentDomain.SetData("DataDirectory", "C:\myDB");

// SQL Connection String with |DataDirectory| substitution string
SqlConnection c = new SqlConnection (
   @"Data Source=.\SQLDB; AttachDbFilename=|DataDirectory|\Database.mdf;Initial Catalog=Master");



回答2:


In the MSDN social forums this answer can be found

|DataDirectory| (enclosed in pipe symbols) is a substitution string that indicates the path to the database. It eliminates the need to hard-code the full path which leads to several problems as the full path to the database could be serialized in different places. DataDirectory also makes it easy to share a project and also to deploy an application.

For example, instead of having the following connection string:

"Data Source= c:\program files\MyApp\Mydb.sdf"

Using DataDirectory, you can have the following connection string:

“Data Source = |DataDirectory|\Mydb.sdf”

To set the DataDirectory property, call the AppDomain.SetData method. If you do not set the DataDirectory property, the following default rules will be applied to access the database folder:

  • For applications that are put in a folder on the user's computer, the database folder uses the application folder.
  • For applications that are running under ClickOnce, the database folder uses the specific data folder that is created.



回答3:


Incorrect guys! The |DataDirectory| refers to the mssql\data directory your instance is configured for.

So for example I am using Visual Studio 2012 inconjunction with SQL Express. |DataDirectory| places all MDF files under C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA where my sql express was installed not my solutions app_data folder.

Also the file is names MVCMovie.Models.MovieDBContext not Movies.mdf as specified in my web.config.

I'm thinking it needs to be configured somewhere in visual studio for it to be placed appropriately under app_data.




回答4:


There is an internal class called SqlConnectionHelper which parses this and creates the MDF if needed.

Here's the only MS doc I can find about that class and the |DataDirectory| macro: http://msdn.microsoft.com/en-us/library/aa478948.aspx.




回答5:


http://msdn.microsoft.com/en-us/library/aa478948.aspx

The |DataDirectory| portion of the connection string specifies that the MDF file is located in the App_Data directory.




回答6:


This might be relevant if you're using code first migrations.

With VisualStudio 2013 (at least) when running an Update-Database command the data directory is relative from the "Startup Project" currently configured in visual studio.

Even if you run the Update-Database on another project (as selected on the Package Manager Console), it will create your database on the App_Data of the currently selected Startup Project.



来源:https://stackoverflow.com/questions/1409358/ado-net-datadirectory-where-is-this-documented

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!