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

minty picture minty · Nov 24, 2008 · Viewed 16.6k times · Source

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...?

Answer

AJ. picture AJ. · Nov 24, 2008

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).