使用System.Data.SQLite,如何使用相对路径在连接字符串中指定数据库文件

想要在不同的服务器上部署我的项目,我希望能够使用相对路径指定连接字符串。 我似乎无法让它工作,并想知道是否有一些技巧…?

一条建议

您可以在应用程序中构建绝对路径并将其传递给连接字符串。

因此,如果您知道数据库文件位于应用程序文件夹的database子文件夹中,则可以执行以下操作(C#):

  string relativePath = @"database\myfile.s3db"; string currentPath; string absolutePath; string connectionString; currentPath = System.Reflection.Assembly.GetExecutingAssembly().Location; absolutePath = System.IO.Path.Combine(currentPath,relativePath); connectionString = string.Format("DataSource={0}", absolutePath); SQLiteConnection cnn = new SQLiteConnection(connectionString); 

(有人可以纠正我如何获得当前路径)。

这个怎么样?

 "Data Source=|DataDirectory|mydb.db;..." 

我相信|DataDirectory| 指向您的应用所在的目录。 我使用NHibernate,它可以使用以下内容:

  

像这样:

String currentPath = System.IO.Path.GetDirectoryName(Application.ExecutablePath);