I've just finished my project in Visual Basic 2010. I'm using MS Access as my database.
I've saved it in my documents and connected to my project. I didn't copy it to debug.
Now when I create an installer and install it to another computer, an error occurs because it couldn't find my database. Manually copying my database to the other computer solves the problem.
c:\users\users\documents\database.accdb
The problem how could I create an installer including my database? And where should I put my database? Putting my database in the user folder makes it read only.
I use Visual Studio installer, setup project. thanks for your help
My personal philosophy is that installers should not create per-user data or objects. Your application's executable, not the installer, should be the one that creates the .accdb
file (or copies it from a read-only template file which should be in %programfiles%
).
If the database is per-user and should not be manipulated by users directly then it should go under %appdata%\YourCompany\YourProduct\
or (%localappdata%
if it shouldn't roam) instead of My Documents
.
If the database is shared by multiple users, then it should go under %programdata%
(aka %allusersprofile%
).
So do something like this:
- Do not "install" the
.accdb
file during installation.
- Instead, either:
- Use ADO's
CreateDatabase
in your software code.
- Or have a read-only template/prototype database in your Program's installation directory (under
%programfiles%\You
)
- On application start-up, check to see if the
%appdata%\You\Foo.accdb
file exists
- If not, use
File.Copy
to copy the file from %programfiles%\You
to %appdata%
)
- And clear the read-only flag
The correct place for the database is a subfolder of the COMMONAPPLICATIONDATA folder (or if a single user is involved in accessing your db APPLICATIONDATA).
THis is a place where all the data of an application should reside. The path could be retrieved using the Environment.SpecialFolder enum
Dim commonPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
Dim myAppDataPath = Path.Combine(commonPath, "MyAppDataPath")
if Not Directory.Exists(myAppDataPath) Then
Directory.CreateDirectory(myAppDataPath)
End If
Dim myDbPath = Path.Combine(myAppDataPath, "MyDatabaseName.accdb")
This is a simple script that shows how to retrieve this path from your code (of course you could use a simple line in the App.Config file for the connection string)
The code above should already been implemented by your setup program and at your disposal through some predefined macro. It depends by your setup program.
For example, this question/answer details how to reach it through a Visual Studio 2010 Setup project.
During publishing (Compilation) the compiler asks you if the database should be made part of the project. If you answer yes then the program will install the database on the
C-drive and I do not worry about the path. The user will not be able to manipulate the database though.