Fixed a bug that caused the ModifyRecord form to crash on load if the cursor was over the top left hand header cell. Set a timeout for the restore SQL statement to hopefully prevent a timeout exception from being thrown. Minor code clean up.

This commit is contained in:
2018-04-04 01:47:02 -05:00
parent 65dd82cd31
commit f2fe8c592f
10 changed files with 45 additions and 62 deletions
@@ -31,5 +31,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.0.0.1")]
[assembly: AssemblyFileVersion("1.0.0.1")]
+19 -32
View File
@@ -1,5 +1,4 @@
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlClient;
namespace AdvertisingProfitControlData
{
@@ -14,39 +13,27 @@ namespace AdvertisingProfitControlData
{
using (var conn = new SqlConnection(connectionString))
{
// 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).
//if (db.Database.Connection.State == ConnectionState.Open)
//{
// db.Database.Connection.Close();
//}
var query =
@"BEGIN TRY
DECLARE @DefaultDataPath varchar(max)
DECLARE @DefaultLogPath varchar(max)
SELECT @DefaultDataPath = CONVERT(varchar(max),SERVERPROPERTY('INSTANCEDEFAULTDATAPATH'))
SELECT @DefaultLogPath = CONVERT(varchar(max),SERVERPROPERTY('INSTANCEDEFAULTLOGPATH'))
SELECT @DefaultDataPath = CONCAT(@DefaultDataPath, N'" + sqlConStrBuilder.InitialCatalog + @".mdf')
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,
MOVE '" + sqlConStrBuilder.InitialCatalog + @"' TO @DefaultDataPath,
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)
END CATCH";
@"BEGIN TRY
use master
ALTER DATABASE " + sqlConStrBuilder.InitialCatalog + @" SET SINGLE_USER
DROP DATABASE AdvertisingProfitControl
RESTORE DATABASE " + sqlConStrBuilder.InitialCatalog + @" FROM Disk='" + databaseFilePath + @"' with checksum
END TRY
BEGIN CATCH
DECLARE @Error VARCHAR(max)
SELECT @Error = ERROR_MESSAGE()
ALTER DATABASE " + sqlConStrBuilder.InitialCatalog + @" SET MULTI_USER
RAISERROR(@Error, 11, 1)
END CATCH
ALTER DATABASE " + sqlConStrBuilder.InitialCatalog + @" SET MULTI_USER
";
//TODO: Verify that the database is good with DBCC command.
conn.Open();
var restoreCmd = new SqlCommand(query, conn);
var restoreCmd = new SqlCommand(query, conn)
{
CommandTimeout = 300
};
restoreCmd.ExecuteNonQuery();
conn.Close();
return true;