/// <summary> /// Restore database /// </summary> /// <param name="fileName">Target file name</param> public override void RestoreBackup(string fileName) { string masterConnectionString = NopSqlDataHelper.GetMasterConnectionString(_sqlConnectionString); Database db = NopSqlDataHelper.CreateConnection(masterConnectionString); string dbName = NopSqlDataHelper.GetDatabaseName(_sqlConnectionString); string commandText = string.Format( "DECLARE @ErrorMessage NVARCHAR(4000)\n" + "ALTER DATABASE [{0}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE\n" + "BEGIN TRY\n" + "RESTORE DATABASE [{0}] FROM DISK = '{1}' WITH REPLACE\n" + "END TRY\n" + "BEGIN CATCH\n" + "SET @ErrorMessage = ERROR_MESSAGE()\n" + "END CATCH\n" + "ALTER DATABASE [{0}] SET MULTI_USER WITH ROLLBACK IMMEDIATE\n" + "IF (@ErrorMessage is not NULL)\n" + "BEGIN\n" + "RAISERROR (@ErrorMessage, 16, 1)\n" + "END", dbName, fileName); DbCommand dbCommand = db.GetSqlStringCommand(commandText); db.ExecuteNonQuery(dbCommand); //clear all pools SqlConnection.ClearAllPools(); }
/// <summary> /// Backup database /// </summary> /// <param name="fileName">Destination file name</param> public override void Backup(string fileName) { Database db = NopSqlDataHelper.CreateConnection(_sqlConnectionString); string dbName = NopSqlDataHelper.GetDatabaseName(_sqlConnectionString); string commandText = string.Format( "BACKUP DATABASE [{0}] TO DISK = '{1}' WITH FORMAT", dbName, fileName); DbCommand dbCommand = db.GetSqlStringCommand(commandText); db.ExecuteNonQuery(dbCommand); }