none
Where is DataDirectory ?

    Question

  • Hello,

    I see that one of the new features of SSEv is DataDirectory support.

    Where is DataDirectory pointing to? Is it the same directory as the program ".exe" file?

    Thank you

    Tom

     

     

    Wednesday, September 06, 2006 3:01 PM

Answers

All replies

  • Hi,

     

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

    Please let us know if it answers your question.

    Thanks

    Sachin

    Thursday, September 07, 2006 9:43 AM
  • Sachin,

    Good answer.  Thank you very much for this information.

    Tom

     

    Friday, September 15, 2006 1:37 PM
  • Great info...

     

    thanks.

     

    Saturday, October 04, 2008 5:11 AM
  • Has to be the most concise and correct answer I have ever been given.

     

    Well done Sach!

     

    Thursday, November 20, 2008 5:17 AM
  • Sachin Sinha MSFT said:

    To set the DataDirectory property, call the AppDomain.SetData method.

    Sachin

    Sachin, I have a question regarding your advice here.

    You can find my original question here: http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataproviders/thread/565f3649-48fd-4b7f-b062-df8aee57bcfa
     
    I'd been pointed here but am unsure whether your advice answers my question. In summary: The advice you give here seems to be something a developer would call at runtime to set the data directory, but I'm trying to find out where to set it at design time so that, when using a file based database with the dataset designer, I can get it to work.

    For more info, see the link above?

    Thanks
    cj
    Friday, February 20, 2009 1:16 PM
  • At design time, check in the bin/debug or /release folders.

    Friday, May 01, 2009 10:23 AM
  • I have a user running my Winform application on Parallels for Mac. 

    When calling AppDomain.CurrentDomain.GetData("DataDirectory") , it returns "." so it is never able to find out the correct location and hence return the error

    "The database file cannot be found. Check the path to the database. [ File name = .\XXXX.sdf ]"

    How can I get around this ?

    Monday, January 25, 2010 8:57 PM
  • Crazy as it seems I tired moving my DB around and found it worked in the Debug sub-directory!  Are the developeers of VB quite mad!  I'm pretty disgusted with how difficult VB has become and with the outrageous lack of Help!!!
    Wednesday, November 03, 2010 6:56 AM
  • I have a sql ce db that is deployed to C:\Program Files\MyApp\

    In my MyApp.config connection string is set to “Data Source = |DataDirectory|\Mydb.sdf”.

    Now, I would rather my db exist in C:\ProgramData\MyApp\.

    What should I do to accomplish this?

    Friday, March 04, 2011 5:45 PM
  • AppDomain

     

    .CurrentDomain.SetData("DataDirectory", Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData));

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

    See: http://erikej.blogspot.com/2011/02/using-sql-server-compact-40-with.html


    Visit my SQL Server Compact blog - Please mark as answer, if this was it.
    Friday, March 04, 2011 6:44 PM
    Moderator
  • So, I place that code at the startup of my application or in the initialization of the singleton controlling my data access layer?
    Friday, March 04, 2011 8:30 PM
  • Yes, before any data access will do.
    Visit my SQL Server Compact blog - Please mark as answer, if this was it.
    Saturday, March 05, 2011 8:15 AM
    Moderator
  • The problem is, when you change DataDirectory your user's settings will not be found.
    Thursday, April 21, 2011 12:25 PM
  • Thank you, Sachin. I was able to get my MSTest unit tests running properly by adding the |DataDirectory| to my NHibernate configuration file's connection string.
    Tuesday, May 24, 2011 2:38 PM
  • Hi,

    I want to keep all csv files in separate folder in data directory. So Create a folder called data and put all my files there

    But when I refer that path, it still failing. Any suggestion?

    Here is the code:

    [DeploymentItem("dataByMail.csv""Data"), 

    DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV""|DataDirectory|\\Data\\dataByMail.csv""dataByMail#csv"DataAccessMethod.Sequential)]

    Wednesday, February 11, 2015 7:26 PM