none
What is the DataDirectory in @"Data Source = |DataDirectory|\Northwind.sdf"?

    Question

  • hi,
       In the SQL Server 2005 Compact Edition demo, I saw the thing like this:

    con = new SqlCeConnection(@"Data Source = |DataDirectory|\Northwind.sdf");

    What does the |DataDirectory| means? Is it like marco or something? Does it work on windows?

    I tried it on windows app, in debug window, it does not change to the real data directory.

    Thanks

    Wednesday, May 09, 2007 4:02 PM

Answers

  • |DataDirectory| substitution string support

    |DataDirectory| (enclosed in pipe symbols) is a substitution string that indicates the database path. Therefore, you do not have to include the full path in the code. When you include the full path in the code, you may experience problems because the full database path can be serialized in different locations. The |DataDirectory| substitution string also makes it easy to share a project and to deploy an application.

    For example, if you include the full path in the code, the application can have the following connection string.
    Data Source= c:\program files\MyApp\Mydb.sdf
    If you use the |DataDirectory| substitution string, the application 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 are 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.

     

    (from http://support.microsoft.com/?kbid=920700)

     

    Wednesday, May 09, 2007 4:37 PM

All replies

  • |DataDirectory| substitution string support

    |DataDirectory| (enclosed in pipe symbols) is a substitution string that indicates the database path. Therefore, you do not have to include the full path in the code. When you include the full path in the code, you may experience problems because the full database path can be serialized in different locations. The |DataDirectory| substitution string also makes it easy to share a project and to deploy an application.

    For example, if you include the full path in the code, the application can have the following connection string.
    Data Source= c:\program files\MyApp\Mydb.sdf
    If you use the |DataDirectory| substitution string, the application 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 are 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.

     

    (from http://support.microsoft.com/?kbid=920700)

     

    Wednesday, May 09, 2007 4:37 PM
  • >> 
    Data Source = |DataDirectory|\Mydb.sdf
    To set the DataDirectory property, call the AppDomain.SetData method. <<
     
    How do you set the DataDirecory property?  My VB.NET app runs mostly in bin\Debug but the runtime will run in
    another subdirectory. 

    So what is needed?
     '' *** Set DataDirectory ****
             AppDomain.CurrentDomain.SetData("DataDirectory", "../")

    This DOES NOT WORK.  I still get the error 26, cannot find connection or database?
     I would like to have the DB in the top directory above /bin.
    Tuesday, December 29, 2009 2:51 AM
  • I do not think relative paths are supported, try with a full path instead.
    http://erikej.blogspot.com Erik Ejlskov Jensen - Please mark as answer, if this was it.
    Wednesday, January 06, 2010 12:39 PM
  • Hi all,

    I've been reading this thread with interest.  I'm a student and had similar problem with my app using SQL Server 2008 Express (coding in VB 2008 Express).  I wanted to store my database file in folder named "DataBase" which is a peer of BIN (I think, it's been awhile).  The way I solved it was with the following SUB to make the connection string.  The SUB is in a Module named "modDataBase".

      'CONNECTION STRING VARIABLE
         Dim mStrConn As String

        Friend Sub Mdb_ConnectStringBuild()

        'CREATE CONNECTION STRING.

     

            'Determine directory of executable, move back three, and complete path to Db.

            Dim strPath As String = System.IO.Directory.GetCurrentDirectory()

            For i As Integer = 0 To 2

                strPath = strPath.Remove(strPath.LastIndexOf("\"))

            Next i

            strPath = strPath & "\DataBase\DataBaseFile.mdf;"

     

            mStrConn = "Data Source=.\SQLEXPRESS;AttachDbFilename=" & strPath & _

                       "Integrated Security=True;Connect Timeout=30;User Instance=True"

        End Sub



    I presume that |DataDirectory| is a better way of doing this.

    I've since dumped SQL Server Express and am going to use SQL Server Compact Edition for my app as soon as I'm satisifed that installing SSCe and SSMS won't crash some installed apps on my machine that seem to have their own copy of SQL Server Compact Edition .
    Friday, January 15, 2010 11:53 PM
  • Here is how you do it:

     

    AppDomain.CurrentDomain.SetData( "DataDirectory", AppDomain.CurrentDomain.BaseDirectory + "App_Data" );

    App_Data is the folder hold your database.If your database wasn't store at App_Data, replace App_Data with the folder which is hold your database

     

    Chieu Luu

    Saturday, April 30, 2011 5:10 PM