public static List <DatabaseDefinition> GetDatabasesForServer(ServerDefinition server) { var databases = new List <DatabaseDefinition>(); const string query = "SELECT name FROM master.dbo.sysdatabases"; var builder = new SqlConnectionStringBuilder(server.ConnectionString) { ConnectTimeout = 5 }; using (SqlConnection connection = new SqlConnection(builder.ConnectionString)) { SqlCommand command = new SqlCommand(query, connection); connection.Open(); SqlDataReader reader = command.ExecuteReader(); try { while (reader.Read()) { var dbName = reader["name"] as string; databases.Add(new DatabaseDefinition(dbName, server)); } } finally { // Always call Close when done reading. reader.Close(); } } return(databases); }
private void RemoveServer(ServerDefinition server) { var servers = GetSavedServers(); servers.Remove(server); SaveServers(servers); var nodeToRemove = databaseTreeModel.FirstOrDefault(s => s.Value.ServerDefinition == server); databaseTreeModel.Remove(nodeToRemove); DatabasesTreeView.Items.Refresh(); }
private void AddNodesForServer(ServerDefinition server) { var serverNode = new DatabaseTreeNode { ServerDefinition = server, IsServerNode = true }; try { var databases = QueryRunner.GetDatabasesForServer(server); var nodes = new List <TreeViewModel <DatabaseTreeNode> >(); foreach (var db in databases.OrderBy(db => db.DatabaseName)) { if (Settings.HideSystemDatabases && systemDatabases.Contains(db.DatabaseName)) { continue; } var dbNode = new DatabaseTreeNode { IsServerNode = false, DatabaseDefinition = db, }; var dbNodeViewModel = new TreeViewModel <DatabaseTreeNode>(db.DatabaseName, dbNode); dbNodeViewModel.IsChecked = server.SelectedDatabases.Contains(db.DatabaseName); nodes.Add(dbNodeViewModel); } var serverNodeViewModel = new TreeViewModel <DatabaseTreeNode>($"{server.DisplayName} ({nodes.Count})", serverNode); serverNodeViewModel.Children.AddRange(nodes); serverNodeViewModel.IsExpanded = false; nodes.ForEach(node => node.InitParent(serverNodeViewModel)); databaseTreeModel.Add(serverNodeViewModel); } catch (Exception) { var serverNodeViewModel = new TreeViewModel <DatabaseTreeNode>(server.DisplayName + " (Connection Failed)", serverNode); databaseTreeModel.Add(serverNodeViewModel); } }
private void MenuItem_AddServer_OnClick(object sender, RoutedEventArgs e) { AddServerDialog dialog = new AddServerDialog(); if (dialog.ShowDialog() == true) { var servers = GetSavedServers(); if (servers.Any(s => s.DisplayName == dialog.ServerDisplayName)) { MessageBox.Show("A server already exists with this name.\nChoose a different name and try again.", "", MessageBoxButton.OK, MessageBoxImage.Exclamation); return; } var server = new ServerDefinition(dialog.ServerDisplayName, dialog.ServerConnectionString); AddNodesForServer(server); servers.Add(server); SaveServers(servers); } }
private void AddNodesForServer(ServerDefinition server) { var serverNode = new DatabaseTreeNode { ServerDefinition = server, IsServerNode = true }; try { var databases = QueryRunner.GetDatabasesForServer(server); var serverNodeViewModel = new TreeViewModel <DatabaseTreeNode>(server.DisplayName, serverNode); databaseTreeModel.Add(serverNodeViewModel); foreach (var db in databases) { if (Settings.Default.HideSystemDatabases && systemDatabases.Contains(db.DatabaseName)) { continue; } var dbNode = new DatabaseTreeNode { IsServerNode = false, DatabaseDefinition = db }; var dbNodeViewModel = new TreeViewModel <DatabaseTreeNode>(db.DatabaseName, dbNode); serverNodeViewModel.Children.Add(dbNodeViewModel); } } catch (Exception) { var serverNodeViewModel = new TreeViewModel <DatabaseTreeNode>(server.DisplayName + " (Connection Failed)", serverNode); databaseTreeModel.Add(serverNodeViewModel); } DatabasesTreeView.Items.Refresh(); }
public DatabaseDefinition(string databaseName, ServerDefinition server) { DatabaseName = databaseName; Server = server; }