zoukankan      html  css  js  c++  java
  • 发现数据库对象的依赖关系

    SQL Server Management Studio中有一个很有意思的工具,可以查看某个对象的依赖和被依赖关系。如下图所示

    image image

    假设,我们自己的程序也要实现这样的功能,那么该怎么做呢?

    1. 首先,创建一个项目,添加以下三个引用

    image imageimage

    2. 用如下代码测试

    using System;
    using System.Collections.Generic;
    using System.Text;

    using Microsoft.SqlServer.Management.Smo;

    namespace DiscovDepedency
    {
        class Program
        {
            /// <summary>
            /// 这个程序演示了如何发现数据库对象的依赖关系
            /// 作者:陈希章
            /// </summary>
            /// <param name="args"></param>
            static void Main(string[] args)
            {
                Server svr = new Server("localhost");
                Database db = svr.Databases["Northwind"];

                Table tb = db.Tables["Orders"];

                DependencyWalker walker = new DependencyWalker(svr);//这是检测关系的一个工具

                //检测依赖该对象的所有其他对象
                Console.WriteLine("依赖Orders表的所有对象");
                DependencyTree tree = walker.DiscoverDependencies(new[] { tb }, DependencyType.Children);
                foreach (var item in walker.WalkDependencies(tree))
                {
                    var xpath = item.Urn.XPathExpression;
                    var type = item.Urn.Type;

                    Console.WriteLine("\tType:{0},Name:{1}", type, string.Format("{0}.{1}",xpath.GetAttribute("Schema",type),xpath.GetAttribute("Name",type)));
                }

                Console.WriteLine("Orders表所依赖的其他对象");
                DependencyTree tree2 = walker.DiscoverDependencies(new[] { tb }, DependencyType.Parents);
                foreach (var item in walker.WalkDependencies(tree2))
                {
                    var xpath = item.Urn.XPathExpression;
                    var type = item.Urn.Type;

                    Console.WriteLine("\tType:{0},Name:{1}", type, string.Format("{0}.{1}", xpath.GetAttribute("Schema", type), xpath.GetAttribute("Name", type)));
                }

                Console.Read();

            }
        }
    }

    image

  • 相关阅读:
    poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)
    hdu 1166 敌兵布阵(线段树区间求和)
    队列和栈
    完数的输出
    数据类型
    ASP.NET 图片上传工具类 upload image简单好用功能齐全
    ASP.NET 文件上传类 简单好用
    将form表单元素转为实体对象 或集合 -ASP.NET C#
    .NET框架面向对象分层的个人想理
    .NET VS2012 将代码同步上传到 oschina.net 和 github
  • 原文地址:https://www.cnblogs.com/chenxizhang/p/1488315.html
Copyright © 2011-2022 走看看