Answered Where is DataDirectory ?

  • Wednesday, September 06, 2006 3:01 PM
     
     

    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

     

     

All Replies

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

    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

  • Friday, September 15, 2006 1:37 PM
     
     

    Sachin,

    Good answer.  Thank you very much for this information.

    Tom

     

  • Saturday, October 04, 2008 5:11 AM
     
     

    Great info...

     

    thanks.

     

  • Thursday, November 20, 2008 5:17 AM
     
     

    Has to be the most concise and correct answer I have ever been given.

     

    Well done Sach!

     

  • Friday, February 20, 2009 1:16 PM
     
     
    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, May 01, 2009 10:23 AM
     
     

    At design time, check in the bin/debug or /release folders.

  • Monday, January 25, 2010 8:57 PM
     
     
    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 ?

  • Wednesday, November 03, 2010 6:56 AM
     
     
    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!!!
  • Friday, March 04, 2011 5:45 PM
     
     

    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 6:44 PM
    Moderator
     
     

    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 8:30 PM
     
     
    So, I place that code at the startup of my application or in the initialization of the singleton controlling my data access layer?
  • Saturday, March 05, 2011 8:15 AM
    Moderator
     
     
    Yes, before any data access will do.
    Visit my SQL Server Compact blog - Please mark as answer, if this was it.
  • Thursday, April 21, 2011 12:25 PM
     
     
    The problem is, when you change DataDirectory your user's settings will not be found.
  • Tuesday, May 24, 2011 2:38 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.