0

this is the functionality i want to achieve thru passing the Connection string as parameter

     using (DataConnectionDialog dlg = new DataConnectionDialog())
      {

      DataSource.AddStandardDataSources(dlg);
      DataConnectionDialog.Show(dlg).ToString().Equals("OK");
      DataProvider provider = dlg.SelectedDataProvider;
     BcpConfig.DatabaseClient = provider.Name    ;
      string sqlDatabase = ConfigurationManager.AppSettings[provider.Name];
       BcpConfig..Database = DatabaseFactory.CreateDb(dlg.ConnectionString, sqlDatabase);

above on is my model class which creates the connection and give as connection string path like "Data Source=.;Initial Catalog=Northwind;Integrated Security=True"

Priya
  • 49
  • 1
  • 8
  • how can i achieve the above structure thru passing connection string. give me direction as i am not able to even think about the way. – Priya May 05 '15 at 10:15
  • Maybe this could help: https://msdn.microsoft.com/en-us/library/ms254934%28v=vs.110%29.aspx – Adolfo Perez May 05 '15 at 12:25
  • thank you , but no i am looking for MVVM. what to have in view model class. – Priya May 05 '15 at 13:14
  • In your ViewModel you need to create a property of a new type you create called something like TreeNode class which will have a list of TreeNodes so you can build your hierarchy. Then you can populate your tree with the schema info that you get back from the Connection class (get Schema) – Adolfo Perez May 05 '15 at 13:57
  • Building an efficient Tree data scructure: http://stackoverflow.com/questions/9860207/build-a-simple-high-performance-tree-data-structure-in-c-sharp – Adolfo Perez May 05 '15 at 14:00
  • 1
    Lots of ways. Try one and come back if you have an issue. –  May 05 '15 at 16:41
  • @will. i am new in wpf with MVVM so please help me in finding one which is suitable. Thanks in advance – Priya May 06 '15 at 05:58
  • @Adolfo. your link was helpful but didnt get the solution.Thanks – Priya May 06 '15 at 05:59

1 Answers1

1

This should get you started based on @sa_ddam213 answer here.

The ViewModels:

public class NodeViewModel
{
    public NodeViewModel()
    {
        Children = new ObservableCollection<NodeViewModel>();
    }
    public string Id { get; set; }
    public string Name { get; set; }
    public ObservableCollection<NodeViewModel> Children { get; set; }
}

 public class TreeViewModel
{
    public TreeViewModel()
    {
        BuildTree();
    }

    public TreeViewModel Tree
    {
        get { return this; }
    }

    private void BuildTree()
    {
        string connectionString = GetConnectionString();
        using (var connection = new SqlConnection(connectionString))
        {
            // Connect to the database then retrieve the schema information.
            connection.Open();

            // Get the schema information of Databases in your instance
            DataTable databasesSchemaTable = connection.GetSchema("Databases");


            Items = new ObservableCollection<NodeViewModel>();
            var rootNode = new NodeViewModel
            {
                Name = "Databases",
                Children = new ObservableCollection<NodeViewModel>()
            };
            Items.Add(rootNode);

            IEnumerable<string> databases = GetNameList(databasesSchemaTable.Rows, 0);

            foreach (string dbName in databases)
            {
                var dbNode = new NodeViewModel {Name = dbName};
                rootNode.Children.Add(dbNode);
                if (dbName.ToUpper().Equals("<yourdatabase>"))
                {
                    DataTable table = connection.GetSchema("Tables");
                    IEnumerable<string> tables = GetNameList(table.Rows, 2);

                    var tableNode = new NodeViewModel {Name = "Tables"};
                    dbNode.Children.Add(tableNode);
                    foreach (string tableName in tables)
                    {
                        tableNode.Children.Add(new NodeViewModel {Name = tableName});
                    }
                }
            }
        }
    }

    private IEnumerable<string> GetNameList(DataRowCollection drc, int index)
    {
        return drc.Cast<DataRow>().Select(r => r.ItemArray[index].ToString()).OrderBy(r => r).ToList();
    }

    private static string GetConnectionString()
    {
        // To avoid storing the connection string in your code,
        // you can retrieve it from a configuration file.
        return @"Data Source=<yoursource>;Database=<yourdb>;" +
           "Integrated Security=true;";
    }

    public ObservableCollection<NodeViewModel> Items { get; set; } 
}

The View:

<Window x:Class="DBTree.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:DBTree.ViewModel"
    Title="MainWindow" Width="343" Height="744.625">
<Window.DataContext>
    <local:TreeViewModel></local:TreeViewModel>
</Window.DataContext>
<TreeView ItemsSource="{Binding Tree.Items}">
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type local:NodeViewModel}" ItemsSource="{Binding Children}">
            <TextBlock Text="{Binding Name}"></TextBlock>
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>
</Window>

You will get something very plain like this:

tree

Then you can add styling, color, icons, etc

EDIT: I got rid of the View code-behind and used the TreeViewModel as the ViewModel of the MainWindow

Community
  • 1
  • 1
Adolfo Perez
  • 2,834
  • 4
  • 41
  • 61
  • @ Adolfo:Thanks a ton . since last day i am trying this solution even in seperate project. but it is not working: i have done NodeViewModel class into MODEL, TreeViewModel in ViewModel and MainWindow in VIEW.this is the change i did but it is not working is my binding wrong ? – Priya May 07 '15 at 08:47
  • @Priya which problem are you having? Is it not loading the tree? MainWindow is the V and NodeViewModel and TreeViewModel are just VMs. No Model classes in my solution but that should not affect. – Adolfo Perez May 07 '15 at 10:19
  • @Priya take a look at my updated code. I used `TreeViewModel` as the MainWindow view model and added a Tree property to the `TreeViewModel`. I'm setting that VM as the data context for MainWindow.xaml – Adolfo Perez May 07 '15 at 11:26
  • 1
    A Big Thank you..@Adolofo.it was not binding tree but now i can see it. pretty awesome. – Priya May 07 '15 at 12:43