0

I am developing a C# application that uses a Microsoft mdb Database "OLEDB.12.0" for storing records, all works just fine. However I would like to be able to make a backup of the Database when a user clicks on a button.

At the moment I am using the following code that works when running it through MS Studio 2015, but fails after it is installed.

string DBFile = "Database.mdb" as string;

      //Set Database Filename As Today's Date For Backup And Replace Unwanted Characters
      string ThisDate = DateTime.Now.ToString("M/d/yyyy") as string;
      char[] separators = new char[] { ' ', '/', ',', '\r', '\t', '\n' };

      string[] temp = ThisDate.Split(separators, StringSplitOptions.RemoveEmptyEntries);
      ThisDate = String.Join("_", temp);

      //Open The Save Dialog
      SaveFileDialog openFileDialogDB = new SaveFileDialog();

      openFileDialogDB.InitialDirectory = Application.ExecutablePath.ToString();
      openFileDialogDB.Filter = "mdb files (*.mdb)|*.mdb|All files (*.*)|*.*";
      openFileDialogDB.FilterIndex = 1;
      openFileDialogDB.RestoreDirectory = true;
      openFileDialogDB.FileName = ThisDate;

      if (openFileDialogDB.ShowDialog() == DialogResult.OK)
        {
          //Save Database Backup
          File.Copy(DBFile, openFileDialogDB.FileName, true);
        }

The error I get after installing using Install Shield is.

System.IO.FileNotFoundException: Could not find file 'Database.mdb'. File name: 'Database.mdb'

And after installing using One click install I get.

System.IO.FileNotFoundException: Could not find file 'C:\Users\Username\AppData\Local\Apps\2.0\X27G5OGV.1A8\X58VR‌​M4Y.HGD\lght..tion_0‌​000000000000000_0001‌​.0000_f0c86b36a0494a‌​4c\Database.mdb'

So my question is how do I make a backup of the Database after the application is installed.

The odd part is, if I configure Install Shield to run the application after setup completes and then do a DB backup it works fine. However if I exit and restart the application it again fails

Malcolm
  • 784
  • 3
  • 7
  • 20
  • Local database is mdf right ? mdb is the data file of mdf if I am not mistaken. Should you copy mdf instead of mdb ? – Krishna Apr 19 '17 at 23:46
  • Ah! I did not think of that, will give it a try. Thank you – Malcolm Apr 19 '17 at 23:48
  • Try to detach before you copy – Krishna Apr 19 '17 at 23:49
  • @Malcolm, Need proper path to file `string DBFile = Environment.CurrentDirectory + @"\Database.mdb";` assuming file is in the same directory as executable. – Nkosi Apr 19 '17 at 23:50
  • You can also run normal backup command and specify location in the command – Krishna Apr 19 '17 at 23:50
  • Thank you @Nkosi, I wil give that a try also – Malcolm Apr 20 '17 at 00:02
  • @Malcolm, Where is the file located in the project? – Nkosi Apr 20 '17 at 00:34
  • @Nkosi In the same directory as the executable, I must be missing something, will double check – Malcolm Apr 20 '17 at 00:47
  • What connection string are you using to connect to the database? – Joel Coehoorn Apr 20 '17 at 01:00
  • Do not store your database in the application folder; that will be different depending on how the user installs your application. Also, the application folder gets overwritten, destroying all user data, if the user repairs or uninstalls your application. If you have a seed database in your application, copy it to someplace it won't be deleted like `Environment.SpecialFoldwer.ApplicationData`. [Here is a reply with more information](http://stackoverflow.com/a/13998558/22437). – Dour High Arch Apr 20 '17 at 01:02
  • @Joel Coehoom `Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Database.mdb` – Malcolm Apr 20 '17 at 01:02
  • Thank you @Dour High Arch, that is a good idea :) – Malcolm Apr 20 '17 at 01:04
  • Also, these parts don't add up: `after it is installed/after installing using Install Shield`, `Application.ExecutablePath`, and `the same directory as the executable`. Starting with Windows Vista and later, **regular users do NOT have write access to their application's installed folder!** You need to build your installer to deploy the database to your AppData or All Users App Data folder. – Joel Coehoorn Apr 20 '17 at 01:04
  • @Joel Coehoorn Ok, will give that a go, thank you – Malcolm Apr 20 '17 at 01:06
  • 1
    See this link for where the |DataDirectory| ends up: https://social.msdn.microsoft.com/Forums/sqlserver/en-US/dc31ea59-5718-49b6-9f1f-7039da425296/where-is-datadirectory-?forum=sqlce Or you can call `AppDomain.CurrentDomain.GetData("DataDirectory")` – Joel Coehoorn Apr 20 '17 at 01:06
  • Thank you @Joel Coehoorn, I appreciate the help – Malcolm Apr 20 '17 at 01:07

1 Answers1

0

I have finally figured it out so I am posting my solution here for anyone else who might be having the same problem.

After installing Install Shield for Visual Studio and adding it to your project double click Project Assistant in Solution Explorer and then click on Application Shortcuts.

Now in the side panel under Other Places click on Shortcuts and highlight the Shortcut you wish to edit, then to the right in Working Directory add the following [INSTALLDIR] somehow Install Shield left this blank and even though clicking on a Shortcut the application runs OK but without it I get the error as stated above in my question.

Malcolm
  • 784
  • 3
  • 7
  • 20