App.config connection string relative path

44,076

Solution 1

You can specify a relative path as described in Lefty's answer.

However this will be relative to the current working directory, which will not necessarily be the directory containing your executable.

One way round this is to modify the connection string before using it, e.g.

In app.config:

 connectionString="data source={AppDir}\data\EmailDatabase.sqlite

In your code:

ConnectionStringSettings c = ConfigurationManager.ConnectionStrings[name];    
if (c == null)
{
    ... handle missing connection string ...
}
string fixedConnectionString = c.ConnectionString.Replace("{AppDir}", AppDomain.CurrentDomain.BaseDirectory);
... use fixedConnectionString

Solution 2

Yes it is possible. Try using |DataDirectory|.

In App.config

<add name="SqliteConnectionString" connectionString="Data Source=|DataDirectory|\Database.sqlite;" providerName="System.Data.SQLite"/>

More information on this page https://msdn.microsoft.com/en-us/library/cc716756.aspx

Solution 3

Try using a "." before the first backslash in the data source part of the string.

eg.

data source=.\data\EmailDatabase.sqlite
Share:
44,076
Radu D
Author by

Radu D

Updated on July 09, 2022

Comments

  • Radu D
    Radu D almost 2 years

    I need to set in the app.config the sqlite connection string. I want to set the path relative to the debug/release folders the database file will be copied to those folders.

    <add name="EmailsSQLite" connectionString="data source=c:\Users\Test\Documents\Visual Studio 2008\Projects\TestConsole\Emails\data\EmailDatabase.sqlite" providerName="System.Data.SQLite"/>
    

    and I want to have something like:

    <add name="EmailsSQLite" connectionString="data source=\data\EmailDatabase.sqlite" providerName="System.Data.SQLite"/>
    

    Is that possible?

  • AJ.
    AJ. about 13 years
    the only constraint is you can't parameter-ize the connection string easily if you use an auto-load framework like SubSonic (afaik - hope I'm wrong!) since you only provide a connection setting name (which is read-only by that point); shame you can't use "~" like in web.config
  • AJ.
    AJ. about 13 years
    This will only work if you use the relative path (which points at current working directory) before you show any file open / save type dialogs which switch the cwd - assuming it was set to the executable path to start with (which may not be the case)
  • Seng Cheong
    Seng Cheong about 12 years
    Does this question really answer the question? I have the exact same problem, but it's appearing when I'm using the EDMX designer. It cant' show my data source (which is a SQLite db), because my coworker set it up, and it's using his path.
  • Yar
    Yar over 8 years
    If db doesn't exist you'll get this error: A file activation error occurred. The physical file name '.\Database.mdf' may be incorrect. Diagnose and correct additional errors, and retry the operation. CREATE DATABASE failed. Some file names listed could not be created. Check related errors. but in a full-path method it will create the db for you.
  • murzagurskiy
    murzagurskiy over 7 years
    In App.config i can't use neither {AppDir}` nor %AppDir%. This just creates folder witn exact same name in AppData\Local\JetBrains\Installations\ReSharperPlatformVs14
  • DerpyNerd
    DerpyNerd over 6 years
    This is for a SQLite file inside your project? You probably have to set the Build Action for the file to content so it gets copied to debug/bin or release/bin for this to work