I am using access db which is in bin/debug folder and use it in code like
private static string **_strCon** = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
***Application.ExecutablePath.ToString().Substring(0,
Applicat开发者_运维技巧ion.ExecutablePath.ToString().LastIndexOf('\\')) +
"\\Reporting.accdb***;Jet
OLEDB:Database Password=abc;";
and for reports I use DataSet which is using connectionstring from app.config like
<add name="GarzaReportingSystem.Properties.Settings.ReportingConnectionString"
connectionString="Provider=Microsoft.ACE.OLEDB.12.0;***Data Source=|DataDirectory|\bin
\Debug\Reporting.accdb***;Persist Security Info=True;Jet OLEDB:Database Password=abc" providerName="System.Data.OleDb"/>
PROBLEM:
When I make setup of the project and than install the application. I need to put Reporting.accdb in root folder in order for my forms to work using _strCon. In Other words my Executable path becomes root folder
But for DataSet
I need to put access db in bin/debug folder for it to work when application is installed.
How to solve this problem so both resolve same path like root has folder DataBase and both use that path.
you should not hardcode any bin/debug because in deployment you will have the application running from any other installed path, to get the directory path of the exe do this:
var executingFolder = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
then something like:
var dbPath = System.IO.Path.Combine(executingFolder, "data\MyDbName.mdb");
if you include your MyDbName.mdb
file in the project in a folder called data then you can set in Visual Studio first and in the installer afterwards that such data
subfolder will always be deployed under bin/debug in debug or in the final application installation folder.
You should not deploy a DB in your installation directory: Write access in C:\Program Files
and subdirs requires Admin privileges. You should better copy it in your directory under ProgramData
.
Better yet, if you want different Windows users to have their own copy of the DB: When your program starts, it should look for a copy of the DB in current user's AppData
folder. If there's no copy available, your program should first copy the file from \Program Files\blah\
to user's AppData
.
Answers to this question show how to retrieve the AppData path.
精彩评论