コード例 #1
0
        private void selectPathFrom_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new OpenFileDialog
            {
                AddExtension     = true,
                CheckPathExists  = true,
                Filter           = "Backup (*.bak) | *.bak; | All Files (*.*) | *.*",
                InitialDirectory = this.initialPath,
                Title            = "Specify backup file"
            };
            bool?showDialog = dialog.ShowDialog();

            if (showDialog.Value != true)
            {
                return;
            }

            this.dbPathFrom.Text = dialog.FileName;
            try
            {
                this.bakInfo       = SqlServerManager.Instance.GetDatabasesNameFromBackup(ProfileManager.GetConnectionString(), dialog.FileName);
                this.dbPathTo.Text = System.IO.Path.GetDirectoryName(this.bakInfo.physicalNameMdf);
                this.dbName.Text   = this.bakInfo.dbOriginalName;
                this.fileName.Text = this.bakInfo.GetDatabaseName() + ".mdf";
            }
            catch (Exception ex)
            {
                Log.Warn(string.Format("Cannot get information from '{0}' backup", dialog.FileName), typeof(int), ex);
                WindowHelper.ShowMessage("Cannot get information from backup!", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK);
            }
        }
        private void RestoreDatabases(ImportArgs args)
        {
            if (FileSystem.FileSystem.Local.Directory.Exists(args.temporaryPathToUnpack.PathCombine("Databases")))
            {
                foreach (string file in FileSystem.FileSystem.Local.Directory.GetFiles(args.temporaryPathToUnpack.PathCombine("Databases")))
                {
                    FileSystem.FileSystem.Local.File.Delete(file);
                }
            }

            List <string> backupsPaths = this.ExtractDatabases(args);

            if (backupsPaths.Count == 0)
            {
                return;
            }

            var backupInfo = new SqlServerManager.BackupInfo();

            this.GetPostfixForDatabases(backupsPaths, args.connectionString, ref args.databaseNameAppend);

            foreach (string backup in backupsPaths)
            {
                backupInfo = SqlServerManager.Instance.GetDatabasesNameFromBackup(args.connectionString, backup);
                string dbName = backupInfo.dbOriginalName;

                // dbName = GetDatabaseName(dbName, args.connectionString, ref args.databaseNameAppend);
                dbName = this.GetDatabaseName(dbName, ref args.databaseNameAppend);

                // dbName = dbName + GetDBNameAppend(dbName, args.connectionString, 0);
                SqlServerManager.Instance.RestoreDatabase(dbName,
                                                          args.connectionString,
                                                          backup,
                                                          FileSystem.FileSystem.Local.Directory.GetParent(args.virtualDirectoryPhysicalPath).FullName.PathCombine("Databases"),
                                                          backupInfo);
            }
        }