SQL Server Compact ‘Data Directory’ macro in Connection String – more info needed

connection-stringdatadirectorynetsql serversql-server-ce

So, as described on this msdn page, when you define a Connection String for
SQL Server Compact 3.5, you can use the "Data Directory" macro, like this:

quote from this msdn page:

Data Directory Support
SQL Server Compact 3.5 now supports the Data Directory macro. This means that if you add the string |DataDirectory| (enclosed in pipe symbols) to a file path, it will resolve to the path of the database.

For example, consider the connection string:

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

When using Data Directory, you can instead use the following connection string:

"Data Source = |DataDirectory|\Mydb.sdf"

For more information, see How to: Deploy a SQL Server Compact 3.5 Database with an Application.

However, the 'for more information' link on msdn doesn't actually give any more information.

So my question is:

How does the |Data Directory| macro translate at run time? For WinForm apps, it seems to just give the location of the executable. Or is it more complicated than that?

Best Answer

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.
Related Topic