none
SQL Compact version 4 connection string. RRS feed

  • Question

  • Hi,

    I'm struggling with a connection string that will refer to the sdf file within the application.

    Every time I create the connection it inserts the string as d:\AppLocation\AppName\DataFolder\Database.sdf.

    So the connection string in the app refers to that folder. 

    So, when the app gets deployed it unless it is deployed to that directory the app will never find the sdf file.

    For a vb.net desktop app how do I create a connection string to a file that exists within the applicaiton folder?

     I was thinking something like 

    \DataFolder\Database.sdf. 

    But it errors and cannot find the file.

    Surely with an embedded database the file should exist within the applications structure.

    Any assistance appreciated. I'm sure it is simpulz

    Steve

    Tuesday, October 16, 2018 7:08 PM

Answers

  • If your database is in C:\C:\Program Files (x86)\MyCoolApp\DataFolder and the app is in MyCoolApp folder you can use the following 

    Dim databasePathAndName As String = 
            Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DataFolder", "Database.sdf")

    Where does it go? That depends on if you are using a managed provider e.g. you create a connection object then use it in the connection string when interacting with data. If using TableAdapter use the following as the first line in form load event. The connection string for a TableAdapter has |DataDirectory| in it and the line below will set the path to your database.

    AppDomain.CurrentDomain.SetData("DataDirectory", 
        Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DataFolder"))


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by SteveB_Help2 Wednesday, October 17, 2018 7:25 AM
    Tuesday, October 16, 2018 10:43 PM
    Moderator

All replies

  • If DataFolder is a subfolder of your application, then try the next path:

       |DataDirectory|\DataFolder\Database.sdf

    You can also define the meaning of “DataDirectory” using AppDomain.SetData:


    Tuesday, October 16, 2018 8:03 PM
  • Hello,

     I was thinking something like \DataFolder\Database.sdf. 

    '\' (on front) - mean - root folder on the HD

    '.' - mean - current folder

    '.\DataFolder' - mean - DataFolder in current folder.

    But in some cases - on IIS for example - this didn't work and require fullpath.


    Sincerely, Highly skilled coding monkey.

    Tuesday, October 16, 2018 8:16 PM
  • Hi,

    Thanks for the response.

    So the page says;

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

    I understand the connection string referring to DataDirectory in app.config

    However, not where to place the code. Should it go in form_load or somewhere else

    thx, Steve


    Tuesday, October 16, 2018 9:12 PM
  • If your database is in C:\C:\Program Files (x86)\MyCoolApp\DataFolder and the app is in MyCoolApp folder you can use the following 

    Dim databasePathAndName As String = 
            Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DataFolder", "Database.sdf")

    Where does it go? That depends on if you are using a managed provider e.g. you create a connection object then use it in the connection string when interacting with data. If using TableAdapter use the following as the first line in form load event. The connection string for a TableAdapter has |DataDirectory| in it and the line below will set the path to your database.

    AppDomain.CurrentDomain.SetData("DataDirectory", 
        Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DataFolder"))


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by SteveB_Help2 Wednesday, October 17, 2018 7:25 AM
    Tuesday, October 16, 2018 10:43 PM
    Moderator
  • Hi Karen, thnak you.

    I had used;

    AppDomain.CurrentDomain.SetData("DataDirectory", Directory.GetCurrentDirectory())

    but it pointed elsewhere. 

    This looks good.

    I was surprised that when you create a connection  string that you don't get the option to specify an application folder. Especially if you are going to deploy it.

    Thanks again.

    Steve

    Wednesday, October 17, 2018 7:31 AM