Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ADO.NET |DataDirectory| where is this documented?

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

like image 240
programmernovice Avatar asked Sep 11 '09 06:09

programmernovice


People also ask

Where is DataDirectory in connection string?

The |DataDirectory| portion of the connection string specifies that the MDF file is located in the App_Data directory. Show activity on this post.

What is an ADO Net connection?

An ADO.NET connection manager enables a package to access data sources by using a . NET provider. Typically, you use this connection manager to access data sources such as Microsoft SQL Server.

How does ado net connect to database?

Establish Connection to DatabaseBrowse your database file and click the OK button. After connecting to the new database file create an object of OleDBConnection class in case of a database like Oracle or MS-Access and create an object of SqlConnection class in case of MS-SQL database.

What is AttachDBFileName in connection string?

Using AttachDBFileName and User Instance means that SQL Server is creating a special copy of that database file for use by your program. If you have two different programs using that same connection string, they get two entirely different copies of the database.


2 Answers

|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"); 
like image 114
Alex Avatar answered Sep 24 '22 02:09

Alex


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.
like image 30
MRG Avatar answered Sep 24 '22 02:09

MRG