/// <summary> /// Handles the Click event of the DeleteLoginButton control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param> private void DeleteLoginButton_Click(object sender, RoutedEventArgs e) { var serverPair = this.Servers.FirstOrDefault(s => s.Value.Name == this.uxServers.SelectedItem.ToString()); foreach (var pair in this.sqlStudioSettings.SSMS.ConnectionOptions.ServerTypes) { ServerTypeItem serverTypeItem = pair.Value; var toRemove = serverTypeItem.Servers.Where(server => server.Instance == serverPair.Value.ServerConnectionItem.Instance) .ToList(); foreach (ServerConnectionItem serverConnectionItem in toRemove) { var loginsToDelete = serverConnectionItem.Connections.Where(c => c.UserName == this.uxLogins.SelectedItem.ToString()) .ToList(); foreach (var login in loginsToDelete) { serverConnectionItem.Connections.RemoveItem(login); } } } this.HasChanges = true; this.GetLoginsForSelectedServer(); }
/// <summary> /// Handles the Click event of the DeleteServerButton control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param> private void DeleteServerButton_Click(object sender, RoutedEventArgs e) { var serverPair = this.Servers.FirstOrDefault(s => s.Value.Name == this.uxServers.SelectedItem.ToString()); foreach (var pair in this.sqlStudioSettings.SSMS.ConnectionOptions.ServerTypes) { ServerTypeItem serverTypeItem = pair.Value; var toRemove = serverTypeItem.Servers.Where(server => server.Instance == serverPair.Value.ServerConnectionItem.Instance) .ToList(); foreach (ServerConnectionItem serverConnectionItem in toRemove) { serverTypeItem.Servers.RemoveItem(serverConnectionItem); } } this.Servers.Remove(serverPair.Key); this.HasChanges = true; this.Refresh(false); }
// This is the actual worker function of the program. It deletes entries of // servers in the settings file of SSMS. private static void DoJob() { // Counter for number of deleted server entries int delCntr = 0; // The settings file is a serialized instance of class SqlStudio. // Here it gets deserialized. SqlStudio settings; BinaryFormatter binaryFormatter = new BinaryFormatter(); FileStream inStream = new FileStream(SettingsFile, FileMode.Open); try { settings = (SqlStudio)binaryFormatter.Deserialize(inStream); } catch { ShowMessage(ERR_DESERIALIZATION); return; } finally { inStream.Close(); } // List of server entries to delete List <ServerConnectionItem> toProcess = new List <ServerConnectionItem>(); // Iterate over list of server types in settings file // The list elements are key-value pairs foreach (var pair in settings.SSMS.ConnectionOptions.ServerTypes) { // Retrieve value of current list element ServerTypeItem serverType = pair.Value; // Iterate over list of servers of current type. Add all servers that fits // the search criterium to a list. foreach (ServerConnectionItem server in serverType.Servers) { if (ServerToProcess.Equals("*")) { toProcess.Add(server); } else if (server.Instance.StartsWith(ServerToProcess, StringComparison.InvariantCultureIgnoreCase)) { toProcess.Add(server); } } // Process the list filled above foreach (ServerConnectionItem server in toProcess) { switch (Mode) { // In delete mode delete the server entry from the settings class instance case MODE_DELETE: serverType.Servers.RemoveItem(server); delCntr++; break; // In read mode output the server entry to console window case MODE_READ: Console.WriteLine(server.Instance); break; } } // Clear intermediate list for next iteration of server types toProcess.Clear(); } // In delete mode the changed settings have to be written to the settings file if (Mode == MODE_DELETE) { // Serialize the settings class instance to a file stream FileStream outStream = new FileStream(SettingsFile, FileMode.Create); try { binaryFormatter.Serialize(outStream, settings); } catch { ShowMessage(ERR_SERIALIZATION); return; } finally { outStream.Close(); } // Finally show a status message Console.WriteLine("Deleted entries: {0}", delCntr); } }