Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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:UsersTestDocumentsVisual Studio 2008ProjectsTestConsoleEmailsdataEmailDatabase.sqlite" providerName="System.Data.SQLite"/>

and I want to have something like:

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

Is that possible?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
749 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...