In AppConfig it is possible to use |DataDirectory|
but I can't find any doc ?
The |DataDirectory| portion of the connection string specifies that the MDF file is located in the App_Data directory. Show activity on this post.
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.
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.
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.
|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");
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With