使用.Net在数据流任务中按下映射数据流、按下、任务、Net

2023-09-03 08:58:48 作者:孤独患者

几周前我问了这个问题:

"Pressing" the mapping tabs in the Data Flow Task without opening the solution

大数据分析 第二章 大数据平台

现在我可以使用以下代码在PowerShell中加载包对象:

Add-Type -Path 'C:WindowsMicrosoft.NETassemblyGAC_MSILMicrosoft.SqlServer.ManagedDTSv4.0_15.0.0.0__89845dcd8080cc91Microsoft.SqlServer.ManagedDTS.dll';

$pkg = "C:	est.dtsx";

$app = New-Object -TypeName Microsoft.SqlServer.Dts.Runtime.Application 

$app | Get-Member

$Package = $app.LoadPackage($pkg, $null, $FALSE)

$Package | Get-Member

$Package.Executables

$DataFlowTask = $Package.Executables | Where-Object {$_.('Description') -eq ("Data Flow Task")}; 

我的目标是刷新目标和源的数据流任务中的元数据(映射)。

我想在对象上调用这些方法,我认为这应该会起到作用(参见Link)

instance.AcquireConnections(null);
instance.ReinitializeMetaData();
instance.ReleaseConnections();

但我在上面创建的包对象中找不到数据流对象。包对象中数据流任务的地址是什么?

推荐答案

我不确定如何使用PowerShell完成此操作,但我将提供C#解决方案。

//loop over control flow tasks
foreach (DtsRuntime.Executable tsk in pkg.Executables)
{
    DtsRuntime.TaskHost TH = (DtsRuntime.TaskHost)tsk
    //Getting the data flow task object
    if (TH.InnerObject.ToString() == "System.__ComObject")
    {
        try
        {
            DtsWrapper.MainPipe m = (DtsWrapper.MainPipe)TH.InnerObject;
            DtsWrapper.IDTSComponentMetaDataCollection100 mdc = m.ComponentMetaDataCollection;
            //loop over data flow task components
            foreach (DtsWrapper.IDTSComponentMetaData100 md in mdc)
            {
                //get the OLE DB Destination component                
                if(md.ComponentClassID == app.PipelineComponentInfos["OLE DB Destination"].CreationName)
                {
                 //add your code here
                }             
            }
        }
        catch
        {
        }
    }
}

此代码由以下答案改进而来:Reverse engineering SSIS package using C#

我在PowerShell方面没有太多经验,但我会尝试提供一些有用的链接:

Get-SsisMetadata.ps1 How to create SSIS package with PowerShell?(此链接包含从PowerShell执行C#脚本的方法-它可能会有所帮助)