以编程方式将转换脚本组件添加到ssis包

时间:2014-10-31 10:52:58

标签: ssis

任何人都可以帮助我...如何以编程方式将转换脚本组件添加到ssis包中的数据流任务?

我正在通过C#以编程方式使用API​​来创建一个ssis包。它由一个平面文件源和一个sql表(oledb目标)组成。我必须添加一个脚本组件来生成自动增量数字以填充表格的rownum列。 rownum列不是标识列。

CODE GIVEN ......

    public Microsoft.SqlServer.Dts.Runtime.DTSExecResult CreatePackage(Dictionary<int, int> LenMap)
    {
        Package package;
        try
        {
            package = new Package();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message, ex);
        }
        package.MaximumErrorCount = 4;
        package.Name = this.PackageName;
        /* Add the Data Flow Task */
        package.Executables.Add("STOCK:PipelineTask");
        package.DelayValidation = false;
        /* Get the task host wrapper, and the Data Flow task*/
        TaskHost taskHost = package.Executables[0] as TaskHost;
        MainPipe dataFlowTask = (MainPipe)taskHost.InnerObject;
        /*===================================================================================
          ===============================ERROR LOGGING=======================================
          ===================================================================================*/
        package.LoggingMode = DTSLoggingMode.Enabled;

        //For package logging connection manager (flat file)
        ConnectionManager LogConMgr = package.Connections.Add("FILE");
        LogConMgr.ConnectionString = this.LogFilePath;
        LogConMgr.Name = "SSISLog.txt";
        LogProvider packagelogprovider = package.LogProviders.Add("DTS.LogProviderTextFile.2");
        packagelogprovider.ConfigString = LogConMgr.Name;
        packagelogprovider.Name = "Text File Log Provider";
        packagelogprovider.Description = "Writes log entries for events to a CSV file";

        package.LoggingOptions.SelectedLogProviders.Add(packagelogprovider);
        LoggingOptions packageLogging = package.LoggingOptions;
        packageLogging.EventFilterKind = DTSEventFilterKind.Inclusion;
        packageLogging.EventFilter.Initialize();
        packageLogging.EventFilter = new string[] { "OnError", "OnWarning", "OnInformation", "OnPreExecute", "OnExecute", "Diagnostic", "PipelineComponentTime", "OnExecStatusChanged", "OnPostExecute", "OnProgress", "OnPreValidate", "OnPostValidate" };
        //OnError Event 
        DTSEventColumnFilter OnErrorColumnFilter = new DTSEventColumnFilter();
        OnErrorColumnFilter.Computer = true;
        OnErrorColumnFilter.SourceName = true;
        OnErrorColumnFilter.MessageText = true;
        OnErrorColumnFilter.SourceName = true;
        OnErrorColumnFilter.SourceID = true;
        OnErrorColumnFilter.DataBytes = true;
        packageLogging.SetColumnFilter("OnError", OnErrorColumnFilter);

        //OnWarning Event   
        DTSEventColumnFilter OnWarningColumnFilter = new DTSEventColumnFilter();
        OnWarningColumnFilter.Computer = true;
        OnWarningColumnFilter.SourceName = true;
        OnWarningColumnFilter.MessageText = true;
        packageLogging.SetColumnFilter("OnWarning", OnWarningColumnFilter);

        /*==================================================================================
          ================END ERROR LOGGING=================================================
          ==================================================================================*/

        /*=================================================================================
          =========================== FOR SOURCE CONNECTION ===============================
          =================================================================================*/
        /* Add the Flat File connection*/
        ConnectionManager connectionManagerFlatFile = package.Connections.Add("FLATFILE");
        connectionManagerFlatFile.ConnectionString = this.FlatFileConnString;
        connectionManagerFlatFile.Name = "FlatFile";
        connectionManagerFlatFile.Properties["Format"].SetValue(connectionManagerFlatFile, this.FlatFileColFormat);
        connectionManagerFlatFile.Properties["ColumnNamesInFirstDataRow"].SetValue(connectionManagerFlatFile, this.FirstRowAsCol);
        /* Get native flat file connection */
        RuntimeWrapper.IDTSConnectionManagerFlatFile100 connectionFlatFile =
            connectionManagerFlatFile.InnerObject as RuntimeWrapper.IDTSConnectionManagerFlatFile100;
        /* Specify Column delimeter*/
        connectionFlatFile.HeaderRowDelimiter = Environment.NewLine;
        connectionFlatFile.RowDelimiter = Environment.NewLine;
        connectionFlatFile.DataRowsToSkip = 0;
        connectionFlatFile.HeaderRowsToSkip = RowsToSkip;
        //connectionFlatFile.ColumnNamesInFirstDataRow = this.firstRowAsCol;
        /*Determine the number of columns by reading Mapping Information.*/
        foreach (DictionaryEntry de in (IDictionary)MapInfo)
        {
            if (Int32.Parse(de.Key.ToString()) != 0)
            {
                RuntimeWrapper.IDTSConnectionManagerFlatFileColumn100 flatFileCol =
                connectionFlatFile.Columns.Add() as RuntimeWrapper.IDTSConnectionManagerFlatFileColumn100;
                //sS_AssignColumnProperties(flatFileCol, parts[col], new string(delimit));
                sS_AssignColumnProperties(flatFileCol, de.Value.ToString(), LenMap[Int32.Parse(de.Key.ToString())]);
            }
         }                

        //Correct the last Flat File column delimiter, needs to be NewLine not Comma
        connectionFlatFile.Columns[connectionFlatFile.Columns.Count - 1].ColumnDelimiter = Environment.NewLine;

        // Check if columns generated
        if (connectionFlatFile.Columns.Count == 0)
        {
            throw new ArgumentException(string.Format("No flat file columns have been created. "));
        }

        // Add Flat File source component
        IDTSComponentMetaData100 componentSource = dataFlowTask.ComponentMetaDataCollection.New();
        componentSource.Name = "FlatFileSource";
        componentSource.ComponentClassID = "DTSAdapter.FlatFileSource.2";
        // componentSource.UsesDispositions = true;
        // Get source design-time instance, and initialise component
        CManagedComponentWrapper instanceSource = componentSource.Instantiate();
        instanceSource.ProvideComponentProperties();
        // Set source connection
        componentSource.RuntimeConnectionCollection[0].ConnectionManagerID = connectionManagerFlatFile.ID;
        componentSource.RuntimeConnectionCollection[0].ConnectionManager =
        DtsConvert.GetExtendedInterface(connectionManagerFlatFile);
        // Reinitialize the metadata, 
        instanceSource.AcquireConnections(null);
        instanceSource.ReinitializeMetaData();
        instanceSource.ReleaseConnections();

        IDTSOutput100 output = componentSource.OutputCollection[0];
        Stack<int> ColumnsToRemove = new Stack<int>();
        for (int outCol = 0; outCol < output.OutputColumnCollection.Count; outCol++)
        {
            IDTSOutputColumn100 column = output.OutputColumnCollection[outCol];
            column.ErrorRowDisposition = DTSRowDisposition.RD_IgnoreFailure;
            column.TruncationRowDisposition = DTSRowDisposition.RD_IgnoreFailure;

        }

        /*===================================================================================
         * ===================================Add Row Number Transformation==================
         * ===================================================================================*/

        // Create a package variable to store the row count value

        package.Variables.Add("RowCountVar", false, "User", 0);
        IDTSComponentMetaData100 componentRowNumber = dataFlowTask.ComponentMetaDataCollection.New();
        componentRowNumber.Name = "RowNumberTransformation";
        componentRowNumber.ComponentClassID = "DTSTransform.RowCount";
        CManagedComponentWrapper instanceRowNumber = componentRowNumber.Instantiate();
        instanceRowNumber.ProvideComponentProperties();
        // Set the variable name property
        instanceRowNumber.SetComponentProperty("VariableName", "User::RowCountVar");
        // Connect the two components together
        IDTSPath100 path = dataFlowTask.PathCollection.New();
        path.AttachPathAndPropagateNotifications(componentSource.OutputCollection[0], componentRowNumber.InputCollection[0]);
        /*End Row Number Transformation*/

        /*=================================================================================
          ======================== FOR DESTINATION CONNECTION =============================
          =================================================================================*/

        /* Add the SQL OLE-DB connection*/
        ConnectionManager connectionManagerOleDb = package.Connections.Add("OLEDB");
        //connectionManagerOleDb.ConnectionString = this.OleDbConnString;
        connectionManagerOleDb.ConnectionString = string.Format(
            "Provider=SQLOLEDB;Data Source={0};Initial Catalog={1};User ID=truser;Password=truser", this.DBServerName, this.DBName);

        connectionManagerOleDb.Name = "OLEDB";            
        /*=================================================================================
          ============================Derived Columns======================================
          =================================================================================*/
        //Derived Column
        IDTSComponentMetaData100 derived = dataFlowTask.ComponentMetaDataCollection.New();
        derived.Name = "Derived Column Component";
        derived.ComponentClassID = "DTSTransform.DerivedColumn";
        CManagedComponentWrapper DesignDerivedColumns = derived.Instantiate();
        DesignDerivedColumns.ProvideComponentProperties();        //design time

        derived.InputCollection[0].ExternalMetadataColumnCollection.IsUsed = false;
        derived.InputCollection[0].HasSideEffects = true;

        //update the metadata for the derived columns
        DesignDerivedColumns.AcquireConnections(null);
        DesignDerivedColumns.ReinitializeMetaData();
        DesignDerivedColumns.ReleaseConnections();

        //Create the path from source to derived component
        IDTSPath100 SourceToDerivedPath = dataFlowTask.PathCollection.New();
        SourceToDerivedPath.AttachPathAndPropagateNotifications(componentRowNumber.OutputCollection[0], derived.InputCollection[0]);

        /*Replace Values of Input to null for empty string*/
        // Get the derived's default input and virtual input.
        IDTSInput100 input = derived.InputCollection[0];
        IDTSVirtualInput100 derivedInputVirtual = input.GetVirtualInput();
        IDTSCustomProperty100 property = null;

        // Iterate through the virtual input column collection.
        foreach (IDTSVirtualInputColumn100 vColumn in derivedInputVirtual.VirtualInputColumnCollection)
        {
            DesignDerivedColumns.SetUsageType(input.ID, derivedInputVirtual, vColumn.LineageID, DTSUsageType.UT_READWRITE);
        }
        foreach (IDTSInputColumn100 inputColumn in derived.InputCollection[0].InputColumnCollection)
        {
            inputColumn.Description = string.Format("Override the orginal column {0} with a null value if the string is empty.", inputColumn.Name);

            property = inputColumn.CustomPropertyCollection["Expression"];
            property.Name = "Expression";
            property.Value = string.Format("(DT_STR,{0},1252)(LEN(TRIM([{1}])) == 0 ? (DT_STR,{0},1252)(NULL(DT_STR,{0},1252)) : TRIM([{1}]))", inputColumn.Length, inputColumn.Name);

            property = inputColumn.CustomPropertyCollection["FriendlyExpression"];
            property.Name = "FriendlyExpression";
            property.Value = string.Format("(DT_STR,{0},1252)(LEN(TRIM([{1}])) == 0 ? (DT_STR,{0},1252)(NULL(DT_STR,{0},1252)) : TRIM([{1}]))", inputColumn.Length, inputColumn.Name);
            inputColumn.ErrorRowDisposition = DTSRowDisposition.RD_IgnoreFailure;
            inputColumn.TruncationRowDisposition = DTSRowDisposition.RD_IgnoreFailure;
        }
        /*End Replace Values of Input to null for empty string*/
        //For Batch Id

        IDTSOutputColumn100 myCol = derived.OutputCollection[0].OutputColumnCollection.New();
        myCol.Name = "BtchId";
        myCol.SetDataTypeProperties(Microsoft.SqlServer.Dts.Runtime.Wrapper.DataType.DT_I4, 0, 0, 0, 0);
        myCol.ExternalMetadataColumnID = 0;
        myCol.ErrorRowDisposition = DTSRowDisposition.RD_IgnoreFailure;
        myCol.TruncationRowDisposition = DTSRowDisposition.RD_IgnoreFailure;

        IDTSCustomProperty100 myProp = myCol.CustomPropertyCollection.New();
        myProp.Name = "Expression";
        myProp.Value = "\"" + this.BatchId.ToString() + "\"";

        myProp = myCol.CustomPropertyCollection.New();
        myProp.Name = "FriendlyExpression";
        myProp.Value = "\"" + this.BatchId.ToString() + "\"";
        // For FileNm

        IDTSOutputColumn100 fleNmCol = derived.OutputCollection[0].OutputColumnCollection.New();
        fleNmCol.Name = "CSVFileNm";
        fleNmCol.SetDataTypeProperties(Microsoft.SqlServer.Dts.Runtime.Wrapper.DataType.DT_STR, 1100, 0, 0, 1252);
        fleNmCol.ExternalMetadataColumnID = 0;
        fleNmCol.ErrorRowDisposition = DTSRowDisposition.RD_IgnoreFailure;
        fleNmCol.TruncationRowDisposition = DTSRowDisposition.RD_IgnoreFailure;

        IDTSCustomProperty100 fleNmProp = fleNmCol.CustomPropertyCollection.New();
        fleNmProp.Name = "Expression";
        fleNmProp.Value = " \" " + this.FileName.Replace("\\", "\\\\") + " \"";

        fleNmProp = fleNmCol.CustomPropertyCollection.New();
        fleNmProp.Name = "FriendlyExpression";
        fleNmProp.Value = " \" " + "csvFile.csv" + " \"";

        // For FileDate

        IDTSOutputColumn100 fleDtCol = derived.OutputCollection[0].OutputColumnCollection.New();
        fleDtCol.Name = "FileDt";
        fleDtCol.SetDataTypeProperties(Microsoft.SqlServer.Dts.Runtime.Wrapper.DataType.DT_STR, 50, 0, 0, 1252);
        fleDtCol.ExternalMetadataColumnID = 0;
        fleDtCol.ErrorRowDisposition = DTSRowDisposition.RD_IgnoreFailure;
        fleDtCol.TruncationRowDisposition = DTSRowDisposition.RD_IgnoreFailure;

        IDTSCustomProperty100 fleDtProp = fleDtCol.CustomPropertyCollection.New();
        fleDtProp.Name = "Expression";
        fleDtProp.Value = "\"" + this.FileDate + "\"";
        fleDtProp = fleDtCol.CustomPropertyCollection.New();
        fleDtProp.Name = "FriendlyExpression";
        fleDtProp.Value = "\"" + this.FileDate + "\"";
        package.Variables.Add("RowNumber", false, "User", 1);
        // For RowNum            
        IDTSOutputColumn100 RowNumCol = derived.OutputCollection[0].OutputColumnCollection.New();
        RowNumCol.Name = "RowNum";
        RowNumCol.SetDataTypeProperties(Microsoft.SqlServer.Dts.Runtime.Wrapper.DataType.DT_STR, 3, 0, 0, 1252);
        RowNumCol.ExternalMetadataColumnID = 0;
        RowNumCol.ErrorRowDisposition = DTSRowDisposition.RD_IgnoreFailure;
        RowNumCol.TruncationRowDisposition = DTSRowDisposition.RD_IgnoreFailure;

        IDTSCustomProperty100 RowNumColProp = RowNumCol.CustomPropertyCollection.New();
        RowNumColProp.Name = "Expression";
        RowNumColProp.Value = "DATEPART(\"Ms\",GetDate())"; 
        RowNumColProp = RowNumCol.CustomPropertyCollection.New();
        RowNumColProp.Name = "FriendlyExpression";
        RowNumColProp.Value = "DATEPART(\"Ms\",GetDate())"; 


        /*Script Component*/
        IDTSComponentMetaData100 scriptPropType = dataFlowTask.ComponentMetaDataCollection.New();

        scriptPropType.Name = "Transform Property Type";

        scriptPropType.ComponentClassID = "DTSTransform.ScriptComponent";

        scriptPropType.Description = "Transform Property Type";

        CManagedComponentWrapper instance2 = scriptPropType.Instantiate();

        instance2.ProvideComponentProperties();
        string[] scriptValue = new String[4];

        scriptValue[0] = "dts://Scripts/" + "ScriptComponent_9ddf9eba6f5b488f9710b95e4d7e7c74" + "/" + "ScriptComponent_9ddf9eba6f5b488f9710b95e4d7e7c74" + ".vsaproj";
        scriptValue[1] = Resources.ProjectFile;//????????

        scriptValue[2] = "dts://Scripts/" + "ScriptComponent_9ddf9eba6f5b488f9710b95e4d7e7c74" + "/ScriptMain.vsaitem";
        scriptValue[3] = "/* Microsoft SQL Server Integration Services user script component\r\n"
        + "* This is your new script component in Microsoft Visual Basic .NET \r\n"
        + "* ScriptMain is the entrypoint class for script components*/\r\n"
        + "\r\n"
        + "using System \r\n"
        + "using System.Data \r\n"
        + "using System.Math \r\n"
        + "using Microsoft.SqlServer.Dts.Pipeline.Wrapper \r\n"
        + "using Microsoft.SqlServer.Dts.Runtime.Wrapper \r\n"
        + " \r\n"
        + "public class ScriptMain:UserComponent  \r\n"
        + "{ \r\n"
        + " int i;\r\n"
        + "public override void PreExecute(){\r\n"
        + "base.PreExecute();\r\n"
        + " i=0;\r\n"
        + "}\r\n"
        + "public override void postExecute(){\r\n"
        + "base.postExecute();\r\n"
        + "}\r\n"
        + " public override void Input0_ProcessInputRow(Input0Buffer Row){ \r\n"
        + " ++i;\r\n"
        + " Row.RowNum = i.ToString(); \r\n"
        + " } \r\n"
        + "} \r\n";

        IDTSDesigntimeComponent100 scriptComponentDesignTime = instance2 as IDTSDesigntimeComponent100;

        scriptComponentDesignTime.SetComponentProperty("SourceCode", scriptValue);

        scriptComponentDesignTime.SetComponentProperty("PreCompile", false);

        /*End Script Component
        /*=================================================================================
         ======================== FOR DESTINATION ========================================
         =================================================================================*/
        /*Add OLE-DB destination*/
        IDTSComponentMetaData100 componentDestination = dataFlowTask.ComponentMetaDataCollection.New();
        componentDestination.Name = "OLEDBDestination";
        componentDestination.ComponentClassID = "DTSAdapter.OLEDBDestination.2";
        /* Get destination design-time instance, and initialise component*/
        CManagedComponentWrapper instanceDestination = componentDestination.Instantiate();
        instanceDestination.ProvideComponentProperties();
        /* Set destination connection*/
        componentDestination.RuntimeConnectionCollection[0].ConnectionManagerID = connectionManagerOleDb.ID;
        componentDestination.RuntimeConnectionCollection[0].ConnectionManager =
        DtsConvert.GetExtendedInterface(connectionManagerOleDb);
        /* Set destination table name*/
        instanceDestination.SetComponentProperty("OpenRowset", this.TargetTable);
        instanceDestination.SetComponentProperty("AccessMode", 0);
        //instanceDestination.SetComponentProperty("ProtectionLevel", "SaveSensitive");
        instanceDestination.SetComponentProperty("DefaultCodePage", 1252);
        /* Set Data Flow direction*/
        //Create the path from derived to desitination
        IDTSPath100 DerivedToDestinationPath = dataFlowTask.PathCollection.New();
        DerivedToDestinationPath.AttachPathAndPropagateNotifications(derived.OutputCollection[0], componentDestination.InputCollection[0]);

        /* Get input and virtual input for destination to select and map columns*/
        IDTSInput100 destinationInput = componentDestination.InputCollection[0];
        IDTSVirtualInput100 destinationVirtualInput = destinationInput.GetVirtualInput();
        IDTSVirtualInputColumnCollection100 destinationVirtualInputColumns = destinationVirtualInput.VirtualInputColumnCollection;
        //componentDestination.ValidateExternalMetadata = false;
        /*  Reinitialize the metadata, generating exernal columns from flat file columns
            If errors are raised here, it is most likely because the flat file connection columns 
            are wrong, which itself is probably because the template table does not match the file.*/
        instanceDestination.AcquireConnections(null);
        instanceDestination.ReinitializeMetaData();
        instanceDestination.ReleaseConnections();

        /*=================================================================================
          ======================== MAP DESTINATION ========================================
          =================================================================================*/
        /*Select and map destination columns*/

        foreach (IDTSVirtualInputColumn100 virtualInputColumn in destinationVirtualInputColumns)
        {
        // Select column, and retain new input column
        IDTSInputColumn100 inputColumn = instanceDestination.SetUsageType(destinationInput.ID,
            destinationVirtualInput, virtualInputColumn.LineageID, DTSUsageType.UT_READWRITE);
        // Find external column by name
        IDTSExternalMetadataColumn100 externalColumn =
            destinationInput.ExternalMetadataColumnCollection[inputColumn.Name];
        // Map input column to external column
        instanceDestination.MapInputColumn(destinationInput.ID, inputColumn.ID, externalColumn.ID);
        }

        new Application().SaveToXml("E:\\TrustPortal-All\\SourceCode\\TrustSolution\\Web\\TrustSolution\\TrustRite\\OtherActivity\\Import\\Temp\\test.dtsx", package, null);
        Microsoft.SqlServer.Dts.Runtime.DTSExecResult results = package.Execute();
        this.RowCount = GetRowCount(package);
        string message = "";
        if (results == Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure)
        {

            foreach (Microsoft.SqlServer.Dts.Runtime.DtsError local_DtsError in package.Errors)
            {
                message += local_DtsError.Description.ToString();
            }
        }
        if (results == Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success)
        {
            message = "Package Executed Successfully....";
        }

        package.Dispose();
        return results;
    }
}


    private void sS_AssignColumnProperties(RuntimeWrapper.IDTSConnectionManagerFlatFileColumn100 flatFileCol, string getColName, int colLength)
    {
        //Assign delimiter:            
        flatFileCol.ColumnType = this.FlatFileColFormat; 
        //Indicate column data type – in this case, all the source columns will be set to String Data Type:
        flatFileCol.DataType = RuntimeWrapper.DataType.DT_STR;
        flatFileCol.MaximumWidth = colLength;
        //Indicate column width – in this case, width of all source columns will be set to a length of 50:
        flatFileCol.ColumnWidth = colLength; //flatFileColWidth;
        flatFileCol.DataPrecision = 0;
        flatFileCol.DataScale = 0;
        //Assign column name:
        RuntimeWrapper.IDTSName100 columnName = flatFileCol as RuntimeWrapper.IDTSName100;
        columnName.Name = getColName;
    }

0 个答案:

没有答案