ADO.NET |DataDirectory| where is this documented?

C#ado.netDatadirectory

C# Problem Overview


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

C# Solutions


Solution 1 - C#

|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");

Solution 2 - C#

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.

Solution 3 - C#

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.

Solution 4 - C#

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.

Solution 5 - C#

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.

Solution 6 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionprogrammernoviceView Question on Stackoverflow
Solution 1 - C#AlexView Answer on Stackoverflow
Solution 2 - C#MRGView Answer on Stackoverflow
Solution 3 - C#Paul FordView Answer on Stackoverflow
Solution 4 - C#codekaizenView Answer on Stackoverflow
Solution 5 - C#Joe.PView Answer on Stackoverflow
Solution 6 - C#tggmView Answer on Stackoverflow