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:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user