internal void ShowDialog()
        {
            var dialog = _dialogFactory.CreateConnectionDialog();

            if (dialog == null)
            {
                throw new InvalidOperationException(Resources.EntityDataConnectionDialog_NoDataConnectionDialog);
            }

            RaiseBeforeAddSourcesEvent();
            dialog.AddSources(IsSupportedProvider);
            RaiseAfterAddSourcesEvent();

            dialog.LoadSourceSelection();

            RaiseBeforeShowDialogEvent();
            var dc = dialog.ShowDialog(true);

            RaiseAfterShowDialogEvent();

            if (dialog.SaveSelection &&
                dc != null)
            {
                dialog.SaveProviderSelections();
                dialog.SaveSourceSelection();
            }

            if (dc != null)
            {
                try
                {
                    SelectedExplorerConnection = _dataExplorerConnectionManager.AddConnection(
                        null, dc.Provider, dc.EncryptedConnectionString, true);
                    SelectedConnection = dc;
                }
                catch (XmlException xmlException)
                {
                    // AddConnection() call above can throw an XmlException if the connection cannot be made
                    throw new InvalidOperationException(
                              String.Format(
                                  CultureInfo.CurrentCulture, Resources.EntityDataConnectionDialog_DataConnectionInvalid, xmlException.Message),
                              xmlException);
                }
            }
        }
Пример #2
0
        internal static void ShowNewConnectionDialog(TextBox connectionStringTextBox, DTE dte, ComboBox cmbConnections, bool addSeConnection)
        {
            if (dte == null)
            {
                throw new ArgumentNullException("dte");
            }

            try
            {
                var settings = connectionStringTextBox.Tag != null
          ? new MySqlConnectionStringBuilder(connectionStringTextBox.Tag.ToString())
          : new MySqlConnectionStringBuilder();

                var dlg = connectionStringTextBox.Tag == null ? new ConnectDialog() : new ConnectDialog(settings);

                DialogResult res = dlg.ShowDialog();
                if (res != DialogResult.OK)
                {
                    return;
                }

                if ((MySqlConnection)dlg.Connection == null)
                {
                    return;
                }

                var csb = (MySqlConnectionStringBuilder)((MySqlConnection)dlg.Connection).GetType().GetProperty("Settings", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(((MySqlConnection)dlg.Connection), null);
                if (csb == null)
                {
                    return;
                }

                //make sure we don't have already the same connection
                if (cmbConnections.FindString(string.Format("{0}({1})", csb.Server, csb.Database)) < 0)
                {
                    connectionStringTextBox.Tag = csb.ConnectionString;
                    if (!string.IsNullOrEmpty(connectionStringTextBox.Tag.ToString()) && addSeConnection)
                    {
                        // adding connection to server explorer connections
                        Microsoft.VisualStudio.Shell.ServiceProvider sp = new Microsoft.VisualStudio.Shell.ServiceProvider((IOleServiceProvider)dte);
                        IVsDataExplorerConnectionManager             seConnectionsMgr = (IVsDataExplorerConnectionManager)sp.GetService(typeof(IVsDataExplorerConnectionManager).GUID);
                        seConnectionsMgr.AddConnection(string.Format("{0}({1})", csb.Server, csb.Database), GuidList.Provider, connectionStringTextBox.Tag.ToString(), false);

                        var connections = (List <MySqlServerExplorerConnection>)cmbConnections.DataSource;
                        connections.Add(new MySqlServerExplorerConnection {
                            DisplayName = string.Format("{0}({1})", csb.Server, csb.Database), ConnectionString = csb.ConnectionString
                        });
                        cmbConnections.DataSource    = null;
                        cmbConnections.DataSource    = connections;
                        cmbConnections.ValueMember   = "ConnectionString";
                        cmbConnections.DisplayMember = "DisplayName";
                    }
                }
                cmbConnections.Text          = string.Format("{0}({1})", csb.Server, csb.Database);
                connectionStringTextBox.Text = MaskPassword(csb.ConnectionString);
                connectionStringTextBox.Tag  = csb.ConnectionString;
            }
            catch (Exception ex)
            {
                MySqlSourceTrace.WriteAppErrorToLog(ex, null, "The connection string is not valid.", true);
            }
        }