使用 SMO 获取依赖对象的模式名称

发布于 2024-10-11 06:43:00 字数 2774 浏览 3 评论 0原文

使用 SSIS 中的源脚本组件,我尝试检索依赖于表的所有对象的详细信息。到目前为止,我拥有对象类型和名称,但无法检索架构。有谁知道如何在 SMO 中实现这一目标?

我的脚本组件代码是:

' Microsoft SQL Server Integration Services user script component
' This is your new script component in Microsoft Visual Basic .NET
' ScriptMain is the entrypoint class for script components

Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Pipeline.Wrapper
Imports Microsoft.SqlServer.Dts.Runtime.Wrapper
Imports Microsoft.SqlServer.Management.Smo
Imports Microsoft.SqlServer.Management.Common

Public Class ScriptMain
    Inherits UserComponent

    Public Overrides Sub CreateNewOutputRows()
        '
        '
        '
        Dim TargetSQLServer As Server
        Dim TargetDatabase As Database
        Dim TargetTable As Table
        Dim uc As New UrnCollection()
        Dim dw As New DependencyWalker
        Dim dt As DependencyTree
        Dim dc As DependencyCollection
        Dim dcn As DependencyCollectionNode
        Dim sp As New Scripter
        Dim outputString As String

        TargetSQLServer = New Server("localhost")
        TargetDatabase = TargetSQLServer.Databases("AdventureWorks")


        For Each TargetTable In TargetDatabase.Tables
            ' Exclude these objects 
            If TargetTable.IsSystemObject = False Then
                uc = New UrnCollection()
                uc.Add(TargetTable.Urn)
                sp = New Scripter
                sp.Server = TargetSQLServer

                ' Get dependencies 
                dw = New DependencyWalker
                dw.Server = TargetSQLServer
                dt = dw.DiscoverDependencies(uc, DependencyType.Children)
                sp = New Scripter(TargetSQLServer)

                dc = New DependencyCollection
                dc = sp.WalkDependencies(dt)
                outputString = ""
                For Each dcn In dc
                    Me.Output0Buffer.AddRow()
                    Me.Output0Buffer.Database = TargetDatabase.Name.ToString

                    Me.Output0Buffer.Table = TargetTable.Name.ToString

                    outputString = dcn.Urn.ToString
                    Me.Output0Buffer.Dependency.AddBlobData(Text.Encoding.GetEncoding(1252).GetBytes(outputString))

                    Me.Output0Buffer.ObjectType = dcn.Urn.Type.ToString

                    outputString = dcn.Urn.GetNameForType(dcn.Urn.Type.ToString).ToString
                    Me.Output0Buffer.ObjectName.AddBlobData(Text.Encoding.GetEncoding(1252).GetBytes(outputString))

                    outputString = ""
                    Me.Output0Buffer.Schema.AddBlobData(Text.Encoding.GetEncoding(1252).GetBytes(outputString))
                Next
            End If
        Next

    End Sub

End Class

Using a source script component in SSIS, I am attempting to retreive details of all objects which depend on a table. So far, I have the object type and name but can't retreive the schema. Does anyone know how to acheive this in SMO?

My script component code is:

' Microsoft SQL Server Integration Services user script component
' This is your new script component in Microsoft Visual Basic .NET
' ScriptMain is the entrypoint class for script components

Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Pipeline.Wrapper
Imports Microsoft.SqlServer.Dts.Runtime.Wrapper
Imports Microsoft.SqlServer.Management.Smo
Imports Microsoft.SqlServer.Management.Common

Public Class ScriptMain
    Inherits UserComponent

    Public Overrides Sub CreateNewOutputRows()
        '
        '
        '
        Dim TargetSQLServer As Server
        Dim TargetDatabase As Database
        Dim TargetTable As Table
        Dim uc As New UrnCollection()
        Dim dw As New DependencyWalker
        Dim dt As DependencyTree
        Dim dc As DependencyCollection
        Dim dcn As DependencyCollectionNode
        Dim sp As New Scripter
        Dim outputString As String

        TargetSQLServer = New Server("localhost")
        TargetDatabase = TargetSQLServer.Databases("AdventureWorks")


        For Each TargetTable In TargetDatabase.Tables
            ' Exclude these objects 
            If TargetTable.IsSystemObject = False Then
                uc = New UrnCollection()
                uc.Add(TargetTable.Urn)
                sp = New Scripter
                sp.Server = TargetSQLServer

                ' Get dependencies 
                dw = New DependencyWalker
                dw.Server = TargetSQLServer
                dt = dw.DiscoverDependencies(uc, DependencyType.Children)
                sp = New Scripter(TargetSQLServer)

                dc = New DependencyCollection
                dc = sp.WalkDependencies(dt)
                outputString = ""
                For Each dcn In dc
                    Me.Output0Buffer.AddRow()
                    Me.Output0Buffer.Database = TargetDatabase.Name.ToString

                    Me.Output0Buffer.Table = TargetTable.Name.ToString

                    outputString = dcn.Urn.ToString
                    Me.Output0Buffer.Dependency.AddBlobData(Text.Encoding.GetEncoding(1252).GetBytes(outputString))

                    Me.Output0Buffer.ObjectType = dcn.Urn.Type.ToString

                    outputString = dcn.Urn.GetNameForType(dcn.Urn.Type.ToString).ToString
                    Me.Output0Buffer.ObjectName.AddBlobData(Text.Encoding.GetEncoding(1252).GetBytes(outputString))

                    outputString = ""
                    Me.Output0Buffer.Schema.AddBlobData(Text.Encoding.GetEncoding(1252).GetBytes(outputString))
                Next
            End If
        Next

    End Sub

End Class

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初吻给了烟 2024-10-18 06:43:00

嘿埃科纳,
我有工作代码可以遍历数据库中的依赖关系树,并通过简单的字符串解析解决了问题。

您的骨灰盒将以以下形式退回:

///StoredProcedure[@Name='uspUpdateEmployeeHireInfo' and @Schema='HumanResources']

只需解析@Name,然后解析@Schema。

下载 DBSourceTools 的源代码:http://dbsourcetools.codeplex.com
看看 DBSourceToolsLib.SysObjects.UrnParser
还有 DBSourceToolsLib.SysObjects.SODependencyTree 作为工作示例。

Hey ekoner,
I have working code that walks the dependency tree in databases, and resolved the issue with simple string parsing.

Your urn will be returned is in the form of

///StoredProcedure[@Name='uspUpdateEmployeeHireInfo' and @Schema='HumanResources']

Just parse for @Name and then for @Schema.

Download the source code for DBSourceTools : http://dbsourcetools.codeplex.com
Have a look at DBSourceToolsLib.SysObjects.UrnParser
And also DBSourceToolsLib.SysObjects.SODependencyTree for working examples.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文