Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 423
minty Avatar asked Nov 24 '08 17:11

minty


People also ask

What is connection pooling SQLite?

Connection pooling is a well-known data access pattern. Its main purpose is to reduce the overhead involved in performing database connections and read/write database operations.

Where is my SQLite database file?

The Android SDK provides dedicated APIs that allow developers to use SQLite databases in their applications. The SQLite files are generally stored on the internal storage under /data/data/<packageName>/databases.


2 Answers

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

like image 154
AJ. Avatar answered Sep 21 '22 13:09

AJ.


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;" >
like image 37
trendl Avatar answered Sep 24 '22 13:09

trendl