0

I use this C# code to make the OleDB connection to my access database :

OleDbConnection connection;
connection = new OleDbConnection();
connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.16.0; Persist Security Info=False; Data Source=" + Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\..\Database.accdb");
connection.Open();

The problem is that instead of starting the relative path from the application folder as designated by "AppDomain.CurrentDomain.BaseDirectory", it starts the relative path from the visual studio installation directory.

This obviously causes the following error :

enter image description here

Why does it do that and where in my project settings or other visual studio settings can I set the correct path of my VS project ?

Thanks in advance,

Lantha

Lantha
  • 13
  • 4
  • Why do you have something like this: `Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\..\Database.accdb")` as your path? Use (and define, if required) `|DataDirectory|` instead. Where is your database located? Inside a folder in the Project's directory structure? That path won't exist when you deploy. You can keep a copy of the file in a folder of your Project, but you need to define a `Build Action` (Content) and specify a condition to copy it to the output directory (where you executable *lives*). – Jimi Mar 25 '20 at 13:43
  • If your executable is installed where you cannot write, move the database file to its dedicated AppData sub-folder (or CommonData sub-folder) and update `|DataDirectory|` with `AppDomain.CurrentDomain.SetData("DataDirectory", [The path where you moved your file])` – Jimi Mar 25 '20 at 13:51
  • "Path.Combine..." comes from https://stackoverflow.com/questions/22472713/using-a-relative-path-for-a-database-file-in-c-visual-studio-2013 The database file is in the project's directory structure and the project is NOT in ProgramFiles (x86), it is on another drive in a not write protected folder. – Lantha Mar 25 '20 at 15:18
  • `Path.Combine()` is OK. What's wrong is this: `@"..\..\Database.accdb"`. You cannot have your database inside the Project's directories, except, of course, what's in or below `\bin\Debug` and `\bin\Release`. Above this level, there's nothing that your executable can reach (simply because it doesn't exist when you deploy). As previously described, you can set the File `Build Action`, so it's copied (along with the Folder where it's stored in the Project's structure) to the output directories (usually, the Debug and Release folders) – Jimi Mar 25 '20 at 16:33
  • Thanks for the advice. I managed to find what caused the error. It's actually the windows form conceptor that tries to connect to the database (I don't know why) and with the visual studio executable directory as the working directory. When I actually launch the app, the error doesn't pop up and everything is working. Also meaning that despite what you said Jimi, for some reason my app IS able to access the database file even if it's outside the "bin/debug" folder or even outside the whole projet's directory tree. – Lantha Mar 25 '20 at 18:16
  • To sum it up, my problem is solved for now, I'll get back to you if I get stucked on that again :D Thanks again ! – Lantha Mar 25 '20 at 18:16

0 Answers0