/// <summary>Updates the dialog with info from saved connectios (either ODBC or a recent connection)</summary> /// <param name="dac">Saved connection to use to populate the dialog</param> private void ApplySavedConnection(DataAccessConnection dac) { if (dac != null) { this.tbDatabase.Text = dac.Database; this.cbAuthType.SelectedItem = dac.Auth.ToString(); this.tbUsername.Text = dac.Username; this.tbPassword.Text = dac.Password; this.cbxSavePassword.Checked = !string.IsNullOrEmpty(dac.Password); } }
/// <summary>Loads the recent connections from an XML file</summary> private void GetRecentConnections() { _recentConnections = new List<DataAccessConnection>(); XDocument doc = null; try { string filePath = SETTINGS_PATH + RECENT_CONNECTIONS_FILE; if (File.Exists(filePath)) { doc = XDocument.Load(filePath); List<XElement> xmlConns = doc.Root.Elements().ToList(); foreach (XElement conn in xmlConns) { DataAccessConnection dac = new DataAccessConnection(); ConnectionType type; AuthType auth; Enum.TryParse<ConnectionType>(conn.Descendants("Type").First().Value, out type); Enum.TryParse<AuthType>(conn.Descendants("Auth").First().Value, out auth); dac.Connection = type; dac.Auth = auth; dac.DataSource = conn.Descendants("Source").First().Value; dac.Database = conn.Descendants("Database").First().Value; dac.Username = conn.Descendants("Username").First().Value; string tempPass = conn.Descendants("Password").First().Value; if (!string.IsNullOrEmpty(tempPass)) dac.Password = ToInsecureString(DecryptString(tempPass)); _recentConnections.Add(dac); } } } catch { _recentConnections.Clear(); } }
/// <summary>Handles when Open is clicked. Creates the data connection from the supplied information</summary> private void btnOpen_Click(object sender, EventArgs e) { try { this.Enabled = false; this._dataAccess = DataAccessFactory.GetDataAccess( this.SelectedConnectionType, this.cbDataSource.Text, this.tbDatabase.Text, this.SelectedAuthType, this.tbUsername.Text, this.tbPassword.Text ); DataAccessConnection current = new DataAccessConnection( this.SelectedConnectionType, this.cbDataSource.Text, this.tbDatabase.Text, this.SelectedAuthType, this.tbUsername.Text, (this.cbxSavePassword.Enabled && this.cbxSavePassword.Checked ? this.tbPassword.Text : null) ); int currIndex = _settings.RecentConnections.IndexOf( _settings.RecentConnections.FirstOrDefault(c => c.Connection == current.Connection && c.DataSource == current.DataSource) ); if (currIndex >= 0) { _settings.RecentConnections.RemoveAt(currIndex); } _settings.RecentConnections.Insert(0, current); _settings.Save(); this.DialogResult = System.Windows.Forms.DialogResult.OK; this.Close(); } catch (Exception ex) { MessageBox.Show("Could not open the connection!\r\n" + ex.Message, "Invalid Connection", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { this.Enabled = true; } }