With System.Data.SQLite how do you specify a database file in the connect string using a relative path

送分小仙女□ 提交于 2020-01-19 11:14:58

问题


Wanting to deploy my project on different servers I would prefer to be able to specify a connect string using a relative path. I can't seem to get that to work and want to know if there is some trick to it...?


回答1:


A suggestion

You could build the absolute path in the app and pass that in the connection string.

So, if you know that the database file is in the database subfolder of the application folder, you could do something like this (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);

(Someone can probably correct me on how to get the current path).




回答2:


How about this?

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

I believe |DataDirectory| point to the directory where your app is located. I use NHibernate and it works with the following:

<add key="hibernate.connection.connection_string"
       value="Data Source=|DataDirectory|mydb.db;Version=3;Compress=False;synchronous=OFF;" >



回答3:


Like this:

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



来源:https://stackoverflow.com/questions/314853/with-system-data-sqlite-how-do-you-specify-a-database-file-in-the-connect-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!