Exemple #1
0
 public Workspace OpenWorkspace(String path)
 {
     var upgrader = new WorkspaceUpgrader(path);
     int version;
     try
     {
         using (var connection = upgrader.OpenConnection())
         {
             try
             {
                 version = upgrader.ReadSchemaVersion(connection);
             }
             catch (Exception e)
             {
                 Console.Out.WriteLine(e);
                 MessageBox.Show(
                     "Unable to read version number from the workspace.  This workspace may be too old to be upgraded.");
                 return null;
             }
         }
     }
     catch (Exception openException)
     {
         Console.Out.WriteLine(openException);
         MessageBox.Show("Unable to open the database:" + openException.Message);
         return null;
     }
     if (version > WorkspaceUpgrader.CurrentVersion || version < WorkspaceUpgrader.MinUpgradeableVersion)
     {
         MessageBox.Show("This workspace cannot be opened by this version of Topograph", Program.AppName);
         return null;
     }
     if (version < WorkspaceUpgrader.CurrentVersion)
     {
         var result =
             MessageBox.Show(
                 "This workspace needs to be upgraded to this version of Topograph.  Do you want to do that now?",
                 Program.AppName, MessageBoxButtons.OKCancel);
         if (result == DialogResult.Cancel)
         {
             return null;
         }
         using (var longWaitDialog = new LongWaitDialog(TopLevelControl, "Upgrading Workspace"))
         {
             if (!new LongOperationBroker(upgrader.Run, longWaitDialog).LaunchJob())
             {
                 return null;
             }
         }
     }
     return new Workspace(path);
 }
        private void BtnOkOnClick(object sender, EventArgs e)
        {
            if (!CheckRequiredField(tbxServer)
                || !CheckRequiredField(comboDatabaseName)
                || !CheckRequiredField(tbxUsername)
                || !CheckRequiredField(tbxPassword))
            {
                return;
            }
            Settings.Default.Reload();
            var tpgLinkDef = GetTpgLinkDef();
            var workspaceUpgrader = new WorkspaceUpgrader(tpgLinkDef);
            try
            {
                using (var connection = workspaceUpgrader.OpenConnection())
                {
                    try
                    {
                        workspaceUpgrader.ReadSchemaVersion(connection);
                    }
                    catch
                    {
                        MessageBox.Show(this,
                                        string.Format("The database {0} is not a Topograph workspace",
                                                      tpgLinkDef.Database));
                        return;
                    }
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(this,
                                string.Format("There was an error trying to connect to the database: {0}",
                                              exception), Program.AppName);
                return;
            }
            using (var fileDialog = new SaveFileDialog
                                 {
                                     Filter = TopographForm.OnlineWorkspaceFilter,
                                     InitialDirectory = Settings.Default.WorkspaceDirectory,
                                     FileName = comboDatabaseName.Text + ".tpglnk",
                                 })
            {

                while (true)
                {
                    if (fileDialog.ShowDialog(this) == DialogResult.Cancel)
                    {
                        return;
                    }
                    try
                    {
                        tpgLinkDef.Save(fileDialog.FileName);
                    }
                    catch (Exception exception)
                    {
                        MessageBox.Show(this, string.Format("Error saving .tpglnk file: {0}", exception),
                                        Program.AppName);
                        continue;
                    }
                    Filename = fileDialog.FileName;
                    Settings.Default.WorkspaceDirectory = Path.GetDirectoryName(Filename);
                    break;
                }
            }
            Settings.Default.ConnectionSettings = new ConnectionSettings
                                                      {
                                                          Server = tbxServer.Text,
                                                          Port = tbxPort.Text,
                                                          Username = tbxUsername.Text,
                                                      };
            Settings.Default.Save();
            DialogResult = DialogResult.OK;
            Close();
        }