使用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);
- Visual Studio 2012项目将于2010年开放
- 如何从Visual Studio Team Services运行ASP.NET核心entity framework迁移
- 测试两个IEnumerable 是否具有相同频率的相同值
- LinqKit PredicateBuilder返回所有或非行
- 序列化实现INotifyPropertyChanged的类的实例时出现SerializationException
- 如何从pdf c#获取具有特定颜色的文本
- PHP curl to .NET HttpRequest:文件上传到服务器
- 从列表中选择单个项目
- ASP.net Session_Start方法中的dependency injection