以编程方式创建脚本组件作为源

时间:2014-10-10 20:27:57

标签: c# ssis

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

有谁知道如何让它成为一个Souce?

以下是我正在研究的单一方法的课程:

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类是我创建的自定义类,用于保存创建输出时使用的定义。

1 个答案:

答案 0 :(得分:1)

因此,此问题的解决方案是从组件中删除所有输入。默认情况下,组件具有&#34;输入0&#34;和&#34;输出0&#34;这与Transform脚本组件类型相关。源类型没有输入,目标没有输出。

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

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;
// ...