开发者

Using the SMO DependencyWalker to load TreeView

In a windows forms application, using C# I want to programmtically pass the SMO DependencyWalker a single object ( table, View, etc ), discover the dependencies related to that object and then load a WinForms TreeView with the results where the TreeView looks exactly like the TreeView within the SQL Server 2008 Management Studio "View Dependencies" dialog. An good example would be to view the dependencies of the "Employees" table in the Northwind database with Management Studio. I have used something like the below code snippett to get started :

UrnCollection col = new UrnCollection(); 

foreach (Table table in database.Tables) {
    if(table.Name == "Employees")
       col.Add(table.Urn); 
}

DependencyTree tree = sp.DiscoverDependencies(col, DependencyType.Children); 
DependencyWalker walker = new DependencyWalker(server); 
DependencyCollection depends = walker.WalkDependencies(tree); 

//Iterate over each table in DB in dependent order... 
foreach (DependencyCollectionNode dcn in depends)

This does return me all the dependencies that I would need for my TreeView, but I am unable to see how to iterate the results of the "depends开发者_如何学Python" object or any of the other associted SMO objects so that I would be able to load The TreeView Nodes in the correct order with the correct parent/child node relationships. Maybe I am going about this in the wrong way. Is there anyone out there that has been able to do something like this successfully?


I post this answer to possibly help some one is looking for it.

DependencyCollection is a flat list so you should use DependencyWalker to have tree structure.

Use FirstChild and NextSibling to traverse the tree.

This is my sample code:

private void button2_Click(object sender, EventArgs e)
{
    String srvName = "xxxxxxxxxxxxx";
    String dbName = "yyyyy";
    Server srv = new Server(new ServerConnection() { ServerInstance = srvName });
    Database db = srv.Databases[dbName];

    DependencyWalker dependencyWalker = new DependencyWalker(srv);
    DependencyTree dependencyTree = dependencyWalker.DiscoverDependencies(
        new Urn[] { db.StoredProcedures["test", "dbo"].Urn }, DependencyType.Parents);

    var dep = new DependencyPrinter(dependencyTree);
    treeView1.Nodes.Clear();
    dep.PrintDependency2(treeView1);
    treeView1.ExpandAll();
}

And the code is using this class

class DependencyPrinter
{
    private DependencyTree _dependencyTree;
    public DependencyPrinter(DependencyTree dependencyTree)
    {
        _dependencyTree = dependencyTree;
    }

    public void PrintDependency2(TreeView treeView)
    {
        var n = treeView.Nodes.Add("");
        AddToTreeNode(n, _dependencyTree.FirstChild);
    }

    private void AddToTreeNode(TreeNode treeNode, DependencyTreeNode node)
    {
        treeNode.Text = String.Format("({0}).{1}.{2}.{3}"
            , node.Urn.Type
            , node.Urn.XPathExpression.GetAttribute("Name", "Database")
            , node.Urn.XPathExpression.GetAttribute("Schema", node.Urn.Type)
            , node.Urn.XPathExpression.GetAttribute("Name", node.Urn.Type));

        if (node.FirstChild != null)
        {
            var n = treeNode.Nodes.Add("");
            AddToTreeNode(n, node.FirstChild);
        }

        if (node.NextSibling != null)
        {
            var n = treeNode.Parent.Nodes.Add("");
            AddToTreeNode(n, node.NextSibling);
        }
    }
}


As far as I know, the Dependency Walker retrieves a flat list of objects.
It is not a recursive or composite "tree" structure.
You would have to build that yourself.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜