0

I'm trying to figure out how to publish an application I wrote. We didn't get to publishing in class, and my programming teacher is having some health issues and isn't available right now.

I found the "Publish" option, and can get it to create a folder with an install program... but I open it, and it just opens the program, and spams me infinitely, complaining that my Access database (located in the bin > debug in the build stage) can't be accessed (from some weird path I don't recognize). I tried using WiX, but it gave me an error when I tried to install, saying it doesn't have access to the install folder (running as admin). I've been googling for a few hours, poking at it, exploring, and I'm not getting too far. Can anyone ELI5?

kristianp
  • 5,496
  • 37
  • 56
Aarron Dixon
  • 97
  • 1
  • 9
  • What do you actually want to do? 'Publish' might not be what you think it is. Do you just want to run it locally? Do you want to package it as a .exe? Do you want to host it online? – Jeroen Vannevel Nov 28 '14 at 01:43
  • 1
    Not to sidetrack here but Access is good tool for less tech-savvy users to manage a datastore, but IMHO it doesn't have many advantages when you're using it programatically (and quite a few drawbacks). I suggest you consider adding "explore other database options" to your ToDo list (SQL Express/SQLite/etc). – Basic Nov 28 '14 at 01:49
  • It's a program that needs to be distributed to multiple users, preferably remotely, but I currently don't have access to the university webserver due to my programming teacher being sick. I used access for simplicity, I actually have SQL training, I just wanted to keep things simple for non-programmatic access potential. I guess package it as an exe? Then maybe when I got webserver access later I could transfer it there? Whatever the case, I need to get the Access database in with the project. – Aarron Dixon Nov 28 '14 at 02:14

2 Answers2

3

When you use the "publish" option for desktop apps, VS creates a click-once installer that will place all the files it knows about in the appropriate locations.

Unfortunately, it can't guess which other files your application needs so you need to tell it explicitly.

If you right-click the Project->Properties, go to the Publish tab and click the "Application Files" button, you'll see all files that will be added to the installer.

Next, click "Show all files" at the bottom. Find your database, and change the Publish Status to "Data File".

Note that I've only ever used the Click Once installer to install static files (like images/documentation) that are never modified, only replaced in later releases. I'm not sure whether your (modified) db will be preserved during an update but I suspect not.

If the Click Once install process is too simple for your needs, VS2010 has "Setup Projects" which create more complex installers that support logic/code. For VS2012, the commonly suggested option is Wix. Unfortunately, it's got a steep learning curve but it can do pretty much anything you need.

I believe VS2013 and later have setup projects again through an extension but I haven't tried it myself.

Edit:

The easiest way around this is likely to set the connection string programatically based on where the application is executing from.

Note that as per this answer clickonce apps are usually executed from deep inside the user profile directory (also read the answer below about data directories). It's a side-effect of how ClickOnce works (it wants to install somewhere the user is guaranteed to have write access).

Check if there really is an .mdb in that folder. If not, you need to tweak the installer or the properties for the .mdb. Assuming it's in the same location as the executable, you can tell your application where to find it...

string dbPath = IO.Path.Combine(
           AppDomain.CurrentDomain.BaseDirectory,
           "access.mdb");

string connectionString = String.Format(
           "Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}",
           dbPath);

I'm not sure why you think the database file added by the installer is in a directory directly under C:\. It's possible you're not looking at the file created by the installer.

To avoid confusion, try changing the name from access.mdb to something else (temp.mdb?), build the installer, rename back to access.mdb. Now, when you've installed the application, make sure the file you're looking at is now called temp.mdb. If not, you're looking at the wrong file.

Community
  • 1
  • 1
Basic
  • 26,321
  • 24
  • 115
  • 201
  • The Access file doesn't show up with the "Show all files" option. Doesn't seem to be much there. – Aarron Dixon Nov 28 '14 at 02:41
  • I just adjusted the properties on my access file to make it available, and include it, but when I start the program it just keeps looking for the database in like, users... I don't know where it's getting that path. How do I get it to use the right path? – Aarron Dixon Nov 28 '14 at 03:51
  • _Somewhere_ in your project, you're specifying a path to the database. This is _usually_ a connection string and it's _usually_ in your `.config` file. Something like `Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;User Id=admin; Password=pass;`. You can either use relative paths (eg just `mydatabase.mdb`) so it looks in the same folder as your executable or set the connection string programatically (usually when you set up whichever classes use it for the first time). Try searching your code for `.mdb` to find where it's configured – Basic Nov 28 '14 at 08:16
  • If you can't find it, can you please edit your question to include an example of how you're actually using the database in code (specifically how you create your context the first time) and we can go from there. NB: If you find the path to the `.mdb` in a file with `Designer` in the filename, it means it's been set through the graphical form editor (eg look at controls on the form(s)). Incidentally, this is one reason to mention project type in your question as "application" could refer to anything from a desktop app to windows service (I've even seen some people use it for websites). – Basic Nov 28 '14 at 08:20
  • It's string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=access.mdb" but it's looking somewhere DEEP in users, and all the executables are sitting in a directory right on C:... so I have no idea why it's looking where it's looking, or how to stop it. – Aarron Dixon Nov 28 '14 at 14:58
  • I've edited my answer. Note that I corrected a mistake which said use EntryAssembly.Location as that includes the executable name too. I've switched to `AppDomain.CurrentDomain.BaseDirectory` as it avoids an additional call to `IO.Path.GetDirectoryName`. Let me know if that helps – Basic Nov 28 '14 at 19:59
0

This Link Has Full demonstration of Database Connectivity And Publish a C# application with database. The application is also running on another machines. How to Publish C# Application with access database

  • Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Pretasoc Jan 13 '19 at 14:32