Created a new form to allow the user to check the database for internal errors. Needs more testing so this build will have the option not visible. Modified the login form to display database tools.

This commit is contained in:
2018-01-13 03:24:59 -06:00
parent 6ec32afc92
commit 2dd754ed29
10 changed files with 6980 additions and 22 deletions
+3 -3
View File
@@ -18,10 +18,10 @@ namespace AdvertisingProfitControlData
public byte State;
public string ErrorMessage = string.Empty;
public bool SingleFileBackup(string optionalBackupFilePath = "")
public bool SingleFileBackup(string connectionString, string optionalBackupFilePath = "")
{
var defaultBackupFile = DefaultBackupLocation + DateTime.Now.ToString("yy-MM-dd") + "." + DefaultBackupExtension;
var sqlConStrBuilder = new SqlConnectionStringBuilder(AdvertisingProfitControlDbContext.ConnectionString);
var sqlConStrBuilder = new SqlConnectionStringBuilder(connectionString);
try
{
using (var sqlConnection = new SqlConnection(sqlConStrBuilder.ConnectionString))
@@ -33,7 +33,7 @@ namespace AdvertisingProfitControlData
State += e.Errors[e.Errors.Count - 1].State % 5 == 0 ? e.Errors[e.Errors.Count - 1].State : (byte) 0;
}
};
//Note: To get a SqlException an error must be raised at level 16 or higher.
//Note: To get a SqlException an error must be raised at level 11 or higher.
//Checksum command needs to be applied in order to properly verify the media as pointed out here:
//http://www.edwinmsarmiento.com/the-scariest-lie-we-believed-about-backup-verification/
//RAISERROR(message string, level, state) levels above 11 throw SQLExceptions.
+13 -7
View File
@@ -1,4 +1,5 @@
using System.Data.SqlClient;
using System.Data;
using System.Data.SqlClient;
namespace AdvertisingProfitControlData
{
@@ -6,17 +7,19 @@ namespace AdvertisingProfitControlData
{
public string ErrorMessage = string.Empty;
public bool SingleFileRestore(string databaseFilePath, string defaultServerName)
public bool SingleFileRestore(string databaseFilePath, string connectionString)
{
var sqlConStrBuilder = new SqlConnectionStringBuilder(AdvertisingProfitControlDbContext.ConnectionString);
var masterConnection = @"data source=" + defaultServerName + @";integrated security=True;MultipleActiveResultSets=True;App=EntityFramework";
var sqlConStrBuilder = new SqlConnectionStringBuilder(connectionString);
try
{
using (var conn = new SqlConnection(masterConnection))
using (var conn = new SqlConnection(connectionString))
{
var db = new AdvertisingProfitControlDbContext();
// var db = new AdvertisingProfitControlDbContext();
//Drop the database to make sure it can be recreated and restored (necessary since the database MUST be on line for its file to be wiped from disk).
db.Database.Delete();
//if (db.Database.Connection.State == ConnectionState.Open)
//{
// db.Database.Connection.Close();
//}
var query =
@"BEGIN TRY
DECLARE @DefaultDataPath varchar(max)
@@ -27,6 +30,8 @@ namespace AdvertisingProfitControlData
SELECT @DefaultLogPath = CONCAT(@DefaultLogPath, N'" + sqlConStrBuilder.InitialCatalog + @".ldf')
IF db_id(N'" + sqlConStrBuilder.InitialCatalog + @"') IS NOT NULL
BEGIN
ALTER DATABASE " + sqlConStrBuilder.InitialCatalog + @" SET SINGLE_USER WITH ROLLBACK IMMEDIATE
DROP DATABASE " + sqlConStrBuilder.InitialCatalog + @"
CREATE DATABASE " + sqlConStrBuilder.InitialCatalog + @"
END
RESTORE DATABASE " + sqlConStrBuilder.InitialCatalog + @" FROM DISK ='" + databaseFilePath + @"' WITH CHECKSUM, REPLACE,
@@ -34,6 +39,7 @@ namespace AdvertisingProfitControlData
MOVE '" + sqlConStrBuilder.InitialCatalog + @"_log' TO @DefaultLogPath
END TRY
BEGIN CATCH
ALTER DATABASE " + sqlConStrBuilder.InitialCatalog + @" SET MULTI_USER
DECLARE @Error varchar(max)
SELECT @Error = ERROR_MESSAGE()
RAISERROR(@Error, 11, 1)