【问题标题】:Create Script Component as Source Programmatically以编程方式创建脚本组件作为源
【发布时间】:2014-10-13 22:31:58
【问题描述】:

我正在以编程方式创建一个包,其中包含一个包含脚本组件作为源的数据流任务。我已经能够创建包、数据流任务并添加脚本组件。但是,脚本组件似乎默认为转换。

有人知道如何让它成为一个源吗?

这是我正在使用的单一方法的课程:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DynamicPackageCreator.Models;
using Microsoft.SqlServer.Dts.Runtime;
using System.IO;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
// Alias to prevent ambiguity
using dtsColumnDataType = Microsoft.SqlServer.Dts.Runtime.Wrapper.DataType;

namespace DynamicPackageCreator
{
    public class DtsClient
    {
        public void CreatePackageWithDataFlowAndScriptSource(string filePath, string dataFlowName, string sourceName, List<OutputDefinition> outputDefinitions)
        {
            // Create the Package
            Package pkg = new Package();
            pkg.Name = Path.GetFileNameWithoutExtension(filePath);

            // Create the Dataflow task
            Executable e = pkg.Executables.Add("STOCK:PipelineTask");
            TaskHost thMainPipe = e as TaskHost;
            thMainPipe.Name = dataFlowName;
            MainPipe dataFlowTask = thMainPipe.InnerObject as MainPipe;

            // Create Source Component
            IDTSComponentMetaData100 sourceComponent = dataFlowTask.ComponentMetaDataCollection.New();
            sourceComponent.Name = sourceName;
            sourceComponent.ComponentClassID = SsisComponentType.ScriptComponent.GetComponentClassId();

            // Get the design time srcDesignTime of the component
            CManagedComponentWrapper srcDesignTime = sourceComponent.Instantiate();

            // Initialize the component
            srcDesignTime.ProvideComponentProperties();

            int lastOutputId = 0;
            // Add metadata
            foreach (var outputDefinition in outputDefinitions)
            {
                var output = srcDesignTime.InsertOutput(DTSInsertPlacement.IP_AFTER, lastOutputId);
                output.Name = outputDefinition.OutputName;
                lastOutputId = output.ID;

                var outputColumnCollection = output.OutputColumnCollection;
                foreach (var outputColumnDefinition in outputDefinition.OutputColumnDefinitions)
                {
                    var outputColumn = outputColumnCollection.New();
                    outputColumn.Name = outputColumnDefinition.ColumnName;
                    outputColumn.SetDataTypeProperties(dtsColumnDataType.DT_WSTR, outputColumnDefinition.ColumnSize, 0, 0, 0);
                }
            }

            // Reinitialise the metadata
            srcDesignTime.ReinitializeMetaData();

            // Save the package
            Application app = new Application();
            app.SaveToXml(filePath, pkg, null);
        }
    }
}

OutputDefinition 类是我创建的自定义类,用于保存创建输出时使用的定义。

【问题讨论】:

    标签: c# ssis


    【解决方案1】:

    因此,解决此问题的方法是从组件中删除所有输入。默认情况下,该组件有一个“输入 0”和一个“输出 0”,这与转换脚本组件类型相关。源类型没有输入,目标没有输出。

    要删除输入和输出,请添加:

    sourceComponent.OutputCollection.RemoveAll();
    sourceComponent.InputCollection.RemoveAll();
    

    这里:

    // ...
    // Initialize the component
    srcDesignTime.ProvideComponentProperties();
    
    // Remove default inputs and outputs
    sourceComponent.OutputCollection.RemoveAll();
    sourceComponent.InputCollection.RemoveAll();
    
    int lastOutputId = 0;
    // ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-30
      • 2013-02-05
      • 1970-01-01
      • 1970-01-01
      • 2014-09-26
      • 1970-01-01
      • 2017-02-03
      相关资源
      最近更新 更多