Home > front end >  How to include an MS Access DB on the startup path of the setup file of ClickOnce and call it with t
How to include an MS Access DB on the startup path of the setup file of ClickOnce and call it with t

Time:07-15

I built an app in VB.NET integrated with an Access DB. Before deploying the app, I had the standard structure of a Windows Forms project with the bin/Debug folders and the EXE file with the DB file.

Now I have the "Public" folder with the installation file inside like the setup, the application manifest, etc ... In this way, the application EXE (that is into the AppData\Local\Apps) does not find the DB and returns the following error:

System.Data.OleDb.OleDbException (0x80004005):Cannot find file "C:\Users\me\AppData\Local\Apps\2.0\QP63K7B2.Q6O\QXCJLXG6.VWW\cons..tion_f8ea45aa6e4eea3e_0001.0000_d39d4d46cbe37100\database.accdb".

Because the connection string I use is:

Provider=Microsoft.ACE.OLEDB.12.0; Data source=" & Application.StartupPath & "/database.accdb

The question is: Can I manually put the DB into the "Publish" folder with the installation files and change the connection string to point to that file? Something like that:

Provider=Microsoft.ACE.OLEDB.12.0; Data source=" & Application.**setupPath** & "/database.accdb

CodePudding user response:

Your connection string is wrong. DO NOT do this:

"Provider=Microsoft.ACE.OLEDB.12.0; Data source=" & Application.StartupPath & "/database.accdb"

Do this:

"Provider=Microsoft.ACE.OLEDB.12.0; Data source=|DataDirectory|\database.accdb"

Because there is no code, you can store that in a config file rather than building it at run time. ClickOnce creates a dedicated folder for data files. When you use "|DataDirectory|" in a connection string, it will resolve to the program startup folder in a non-ClickOnce app - that includes while debugging in VS - but it will resolve to that dedicated data folder in a ClickOnce app. It will also resolve to the App_Data folder in an ASP.NET app. If you do it the way you're supposed to, it will just work. Don't try to move the database. Just fix your connection string.

  • Related