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:
@@ -49,13 +49,15 @@
|
||||
<Compile Include="ActualSale.cs" />
|
||||
<Compile Include="AdItem.cs" />
|
||||
<Compile Include="AdSpecial.cs" />
|
||||
<Compile Include="AdvertisingProfitControlModel.cs" />
|
||||
<Compile Include="AdvertisingProfitControlDbContext.cs" />
|
||||
<Compile Include="Backup.cs" />
|
||||
<Compile Include="CostAnaylsi.cs" />
|
||||
<Compile Include="Inventory.cs" />
|
||||
<Compile Include="Invoice.cs" />
|
||||
<Compile Include="Note.cs" />
|
||||
<Compile Include="Projection.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Restore.cs" />
|
||||
<Compile Include="Supplier.cs" />
|
||||
<Compile Include="Taxable.cs" />
|
||||
<Compile Include="Version.cs" />
|
||||
|
||||
+7
-7
@@ -3,24 +3,24 @@ using System.Data.Entity;
|
||||
|
||||
namespace AdvertisingProfitControlData
|
||||
{
|
||||
public class AdvertisingProfitControlModel : DbContext
|
||||
public class AdvertisingProfitControlDbContext : DbContext
|
||||
{
|
||||
private static string _connectionString = string.Empty;
|
||||
internal static string ConnectionString = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes and stores the supplied connection string. This allows calls to
|
||||
/// an overloaded constructor to take no parameters.
|
||||
/// </summary>
|
||||
/// <param name="connectionString">The connection string for the database.</param>
|
||||
public AdvertisingProfitControlModel(string connectionString) : base(connectionString)
|
||||
public AdvertisingProfitControlDbContext(string connectionString) : base(connectionString)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
ConnectionString = connectionString;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates the database context using a previously initialized connection string.
|
||||
/// </summary>
|
||||
public AdvertisingProfitControlModel()
|
||||
public AdvertisingProfitControlDbContext()
|
||||
: base(ValidateConnectionString())
|
||||
{
|
||||
|
||||
@@ -32,12 +32,12 @@ namespace AdvertisingProfitControlData
|
||||
/// <returns></returns>
|
||||
private static string ValidateConnectionString()
|
||||
{
|
||||
if (_connectionString == string.Empty)
|
||||
if (ConnectionString == string.Empty)
|
||||
{
|
||||
throw new OperationCanceledException("A connection string must be specified.");
|
||||
}
|
||||
|
||||
return _connectionString;
|
||||
return ConnectionString;
|
||||
}
|
||||
|
||||
public virtual DbSet<ActualSale> ActualSales { get; set; }
|
||||
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace AdvertisingProfitControlData
|
||||
{
|
||||
public class Backup
|
||||
{
|
||||
public static readonly string DefaultBackupLocation = Path.GetPathRoot(Environment.SystemDirectory) + @"Users\" + Environment.UserName + @"\AppData\Local\APC\Backups\";
|
||||
public static readonly string DefaultBackupExtension = "apcdbbak";
|
||||
public string ErrorMessage = string.Empty;
|
||||
|
||||
public bool SingleFileBackup(string optionalBackupFilePath = "")
|
||||
{
|
||||
var defaultBackupFile = DefaultBackupLocation + DateTime.Now.ToString("yy-MM-dd") + "." + DefaultBackupExtension;
|
||||
var sqlConStrBuilder = new SqlConnectionStringBuilder(AdvertisingProfitControlDbContext.ConnectionString);
|
||||
try
|
||||
{
|
||||
using (var s = new SqlConnection(sqlConStrBuilder.ConnectionString))
|
||||
{
|
||||
var query = $"BACKUP DATABASE {sqlConStrBuilder.InitialCatalog} TO DISK='{defaultBackupFile}'";
|
||||
|
||||
using (var command = new SqlCommand(query, s))
|
||||
{
|
||||
s.Open();
|
||||
command.ExecuteNonQuery();
|
||||
if (optionalBackupFilePath == string.Empty) return true;
|
||||
command.CommandText =
|
||||
$"BACKUP DATABASE {sqlConStrBuilder.InitialCatalog} TO DISK='{optionalBackupFilePath + DateTime.Now.ToString("yy-MM-dd") + "." + DefaultBackupExtension}'";
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (SqlException e)
|
||||
{
|
||||
ErrorMessage = e.Message;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public List<FileInfo> GetSingleFileBackupFilesList()
|
||||
{
|
||||
var directoryInfo = new DirectoryInfo(DefaultBackupLocation);
|
||||
var files = directoryInfo.GetFiles("*." + DefaultBackupExtension);
|
||||
return files.ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
using System.Data.SqlClient;
|
||||
|
||||
namespace AdvertisingProfitControlData
|
||||
{
|
||||
public class Restore
|
||||
{
|
||||
public string ErrorMessage = string.Empty;
|
||||
|
||||
public bool SingleFileRestore(string databaseFilePath)
|
||||
{
|
||||
var sqlConStrBuilder = new SqlConnectionStringBuilder(AdvertisingProfitControlDbContext.ConnectionString);
|
||||
|
||||
try
|
||||
{
|
||||
using (var conn = new SqlConnection(sqlConStrBuilder.ConnectionString))
|
||||
{
|
||||
var query = $"RESTORE DATABASE {sqlConStrBuilder.InitialCatalog} FROM DISK='{databaseFilePath}'";
|
||||
|
||||
conn.Open();
|
||||
var useMasterCommand = new SqlCommand("USE master", conn);
|
||||
useMasterCommand.ExecuteNonQuery();
|
||||
|
||||
var alter1Cmd = new SqlCommand($"ALTER DATABASE {sqlConStrBuilder.InitialCatalog} SET Single_User WITH Rollback Immediate", conn);
|
||||
alter1Cmd.ExecuteNonQuery();
|
||||
|
||||
var restoreCmd = new SqlCommand(query, conn);
|
||||
restoreCmd.ExecuteNonQuery();
|
||||
|
||||
var alter2Cmd = new SqlCommand($"ALTER DATABASE {sqlConStrBuilder.InitialCatalog} SET Multi_User", conn);
|
||||
alter2Cmd.ExecuteNonQuery();
|
||||
conn.Close();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
catch (SqlException e)
|
||||
{
|
||||
ErrorMessage = e.Message;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user