Implemented database backups and restores into the AdvertisingProfitControlData DLL and added a form, DatabaseRecovery, to manage the backups and restores. Updated the installer to include the data DLL file into the install bundle.

This commit is contained in:
2017-06-23 03:12:51 -05:00
parent 308b60bd84
commit 12a6b36370
20 changed files with 5378 additions and 80 deletions
+17 -1
View File
@@ -1,5 +1,8 @@
using System;
using System.Data.SqlClient;
using System.IO;
using System.Security.AccessControl;
using System.Security.Principal;
using System.Windows.Forms;
using AdvertisingProfitControlData;
using Microsoft.Win32;
@@ -13,6 +16,19 @@ namespace AdvertsingProfitControl
public FrmLoginForm()
{
InitializeComponent();
//Check to see if the backup folder exists.
if (!Directory.Exists(Backup.DefaultBackupLocation))
{
Directory.CreateDirectory(Backup.DefaultBackupLocation);
//Create it and set its permissions so SQL can write to it.
//Reference: https://stackoverflow.com/questions/5298905/add-everyone-privilege-to-folder-using-c-net
var sec = Directory.GetAccessControl(Backup.DefaultBackupLocation);
// Using this instead of the "Everyone" string means we work on non-English systems.
var everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
sec.AddAccessRule(new FileSystemAccessRule(everyone, FileSystemRights.Modify | FileSystemRights.Synchronize, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
Directory.SetAccessControl(Backup.DefaultBackupLocation, sec);
}
//Scan the registry for SQL server instances (not the recommended way).
var registryView = Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32;
using (var hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, registryView))
{
@@ -57,7 +73,7 @@ namespace AdvertsingProfitControl
}
ConnectionSuccessful = true;
//Try to initialize the database.
var db = new AdvertisingProfitControlModel(connectionString);
var db = new AdvertisingProfitControlDbContext(connectionString);
//Force the UI thread to draw the label's text so the user can see it.
informationLabel.Text = @"Connection successful! Preparing main form...";
informationLabel.Invalidate();