问题
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?
回答1:
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.
回答2:
Please review this link: 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.
来源:https://stackoverflow.com/questions/1612959/sql-server-compact-data-directory-macro-in-connection-string-more-info-neede