0

I have a little desktop application using a *.db3 database. When I run the application on other machine than mine, the database cannot be found: an absolute path is used.

Currently, in my App.Config file, I have

connectionStrings>
    add name="MyProject.My.MySettings.MyProjectSQLiteConnectionString"
            connectionString="data source="N:\Long path do my DB\MyDB.db3""
            providerName="System.Data.SQLite" />
/connectionStrings>

I would like to replace the data source with something like this (unfortunately not working)

data source =|DataDirectory|\MyDB.db3

How can I enter the application directory (relative path) in my connection string?

Nicolas
  • 1,151
  • 3
  • 15
  • 28
  • 1
    What about using some path like `%APPDATA%`? or let the user change this config from the preference screen of your app? – Steve B Jan 23 '13 at 12:32

1 Answers1

0

Starting with Steve B's comment and later finding this article, I solved my problem doing the following:

  1. On the user machine, the database is copied into the %AppData% directory.
  2. The |DataDirectory| is dynamically changed between Debug and Release mode.

This allows not to change the connection string.

In the meantime, I opted for a MS Access *.accdb database, but here is my code anyway.

Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

#If (Not Debug) Then

    pathMyAppData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\MyApp"

    '"DataDirectory" is used in the Connection String
    AppDomain.CurrentDomain.SetData("DataDirectory", pathMyAppData)


    'The application closes if no database is found
    If Not File.Exists(pathMyAppData & "\MyDB.accdb) Then
        MsgBox("Database not found. Program closes.", MsgBoxStyle.Critical, "Error")
        Me.Close()
    End If

#End If
Nicolas
  • 1,151
  • 3
  • 15
  • 28