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.
我需要在app.config中设置sqlite连接字符串。我想设置相对于调试/发布文件夹的路径,数据库文件将被复制到这些文件夹。
<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?
那可能吗?
3 个解决方案
#1
14
You can specify a relative path as described in Lefty's answer.
您可以指定Lefty的答案中描述的相对路径。
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:
在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
#2
4
Try using a "." before the first backslash in the data source part of the string.
尝试使用“。”在字符串的数据源部分中的第一个反斜杠之前。
eg.
例如。
data source=.\data\EmailDatabase.sqlite
#3
3
Yes it is possible. Try using |DataDirectory|.
对的,这是可能的。尝试使用| DataDirectory |。
In App.config
在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
有关此页面的更多信息,请访问https://msdn.microsoft.com/en-us/library/cc716756.aspx
#1
14
You can specify a relative path as described in Lefty's answer.
您可以指定Lefty的答案中描述的相对路径。
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:
在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
#2
4
Try using a "." before the first backslash in the data source part of the string.
尝试使用“。”在字符串的数据源部分中的第一个反斜杠之前。
eg.
例如。
data source=.\data\EmailDatabase.sqlite
#3
3
Yes it is possible. Try using |DataDirectory|.
对的,这是可能的。尝试使用| DataDirectory |。
In App.config
在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
有关此页面的更多信息,请访问https://msdn.microsoft.com/en-us/library/cc716756.aspx