private void RestoreDatabase(DatabaseBackupInfo db, bool dryRun, ILog log) { var sb = new StringBuilder(); var dbName = SqlUtils.ToBracketedIdentifier(db.Name); var sqlParams = new SqlParamCollection(); sb.AppendFormat( "USE [master]\n" + "IF db_id(@dbName) IS NOT NULL\n" + " ALTER DATABASE {0} SET SINGLE_USER WITH ROLLBACK IMMEDIATE\n" + "RESTORE DATABASE {0} FROM DISK = @backupFile WITH FILE = 1, NOUNLOAD, STATS = 5", dbName); sqlParams.Add("@dbName", SqlDbType.NVarChar).Value = db.Name; sqlParams.Add("@backupFile", SqlDbType.NVarChar).Value = db.BackupFilePath; if (mDatabaseConnection.Relocate) { var names = SqlUtils.GetLogicalAndPhysicalNamesFromBackupFile(mDatabaseConnection, db.BackupFilePath); var i = 0; foreach (var tuple in names) { var logical = string.Format("@l{0}", i); var physical = string.Format("@p{0}", i); sb.AppendFormat(",\n MOVE {0} TO {1}", logical, physical); var destPath = GetRelocation(db.Name, mDatabaseConnection.RelocatePath, tuple.Item2); sqlParams.Add(logical, SqlDbType.NVarChar).Value = tuple.Item1; sqlParams.Add(physical, SqlDbType.NVarChar).Value = destPath; ++i; } } sb.AppendFormat( "\nALTER DATABASE {0} SET MULTI_USER\n", dbName); log.LogFormat("Restoring {0} from {1}", db.Name, db.BackupFilePath); if (!dryRun) { using (var process = SqlUtils.Exec(mDatabaseConnection, db.Name, sb.ToString(), sqlParams)) using (log.IndentScope()) { foreach (var processOutputLine in process.GetOutput()) { log.Log(processOutputLine.Line); } } } log.Log("Database restore completed."); }