12

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

3 Answers 3

7

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

Sign up to request clarification or add additional context in comments.

1 Comment

Yuk, don't use reflection namespace to get the base path. use: AppDomain.CurrentDomain.BaseDirectory
6

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;" >

1 Comment

This would be the proper method, except that it doesnt support designtime, as in, your program will work in debug and in release modes, but when trying to edit the dataset definitions, it crashes out with "unable to connect to the DB". it is a known bug in SQLITe.
2

Like this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.