派生列未映射到输出列?我能怎么做?

时间:2014-06-04 12:48:54

标签: c#-4.0 sql-server-2012 ssis-2012

已成功生成包。但派生列显示错误 - 验证错误。这是一个编程的DataFlowTask Derived Column [2]:尝试解析表达式" Empid"失败并返回错误代码0xC00470A2。表达式无法解析。它可能包含无效元素,也可能格式不正确。可能还存在内存不足错误。 MyProgrammedDataflowTaskWithDerivedColumn.dtsx

贝娄是我的代码 -

//创建一个应用程序             Application app = new Application();             //创建一个包             包pkg = new Package();             //设置一些属性             pkg.Name = @" MyProgrammedDataflowTaskWithDerivedColumn&#34 ;;             //添加到数据库AdventureWorksLT2008R2的连接             ConnectionManager ConnMgrAdvent = pkg.Connections.Add(" OLEDB");             ConnMgrAdvent.ConnectionString =" Data Source = 412-1682; Initial Catalog = Empdb; Provider = SQLNCLI11.1; Integrated Security = SSPI; Auto Translate = False;&#34 ;;             ConnMgrAdvent.Name = @" AdventureWorks2008R2&#34 ;;             ConnMgrAdvent.Description = @"用于OLEDB源的SSIS连接管理器&#34 ;;             //添加到数据库Import_DB的连接             ConnectionManager ConnMgrImport_DB = pkg.Connections.Add(" OLEDB");             ConnMgrImport_DB.ConnectionString =" Data Source = 412-1682; Initial Catalog = stgEmpdb; Provider = SQLNCLI11.1; Integrated Security = SSPI; Auto Translate = False;&#34 ;;             ConnMgrImport_DB.Name = @" Import_DB&#34 ;;             ConnMgrImport_DB.Description = @" OLEDB源的SSIS连接管理器&#34 ;;

        //Adding the dataflow task to the package
        Executable exe = pkg.Executables.Add("STOCK:PipelineTask");
       TaskHost TKHSQLHost = (TaskHost)exe;
        TKHSQLHost.Name = "This is a programmed DataFlowTask";
        MainPipe dataFlowTask = (MainPipe)TKHSQLHost.InnerObject;

        // Create the source component.
        IDTSComponentMetaData100 source =
        dataFlowTask.ComponentMetaDataCollection.New();
        source.ComponentClassID = "DTSAdapter.OleDbSource.3";
        CManagedComponentWrapper srcDesignTime = source.Instantiate();
        srcDesignTime.ProvideComponentProperties();
        // Assign the connection manager.
        if (source.RuntimeConnectionCollection.Count > 0)
        {
            source.RuntimeConnectionCollection[0].ConnectionManager =DtsConvert.GetExtendedInterface(ConnMgrAdvent);
            source.RuntimeConnectionCollection[0].ConnectionManagerID =
                         pkg.Connections["AdventureWorks2008R2"].ID;
        }

        // Set the custom properties of the source.
        srcDesignTime.SetComponentProperty("AccessMode", 0);
        srcDesignTime.SetComponentProperty("OpenRowset", "[dbo].[emp1]");

        // Connect to the data source, and then update the metadata for the source.
        srcDesignTime.AcquireConnections(null);
        srcDesignTime.ReinitializeMetaData();
        srcDesignTime.ReleaseConnections();

        // Create the destination component.
        IDTSComponentMetaData100 destination =
                 dataFlowTask.ComponentMetaDataCollection.New();
        destination.ComponentClassID = "DTSAdapter.OleDbDestination.3";
        CManagedComponentWrapper destDesignTime = destination.Instantiate();
        destDesignTime.ProvideComponentProperties(); 

        // Assign the connection manager.
        destination.RuntimeConnectionCollection[0].ConnectionManager =
                  DtsConvert.GetExtendedInterface(ConnMgrImport_DB);

        if (destination.RuntimeConnectionCollection.Count > 0)
        {
            destination.RuntimeConnectionCollection[0].ConnectionManager =
                       DtsConvert.GetExtendedInterface(ConnMgrImport_DB);
            destination.RuntimeConnectionCollection[0].ConnectionManagerID =
                       pkg.Connections["Import_DB"].ID;
        }

        // Set the custom properties of the destination
        destDesignTime.SetComponentProperty("AccessMode", 0);
        destDesignTime.SetComponentProperty("OpenRowset", "[dbo].[emp1]");

        // Connect to the data source, and then update the metadata for the source.
        destDesignTime.AcquireConnections(null);
        destDesignTime.ReinitializeMetaData();
        destDesignTime.ReleaseConnections();

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

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

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

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

        //Create the path from derived to desitination
        IDTSPath100 DerivedToDestinationPath = dataFlowTask.PathCollection.New();
        DerivedToDestinationPath.AttachPathAndPropagateNotifications(derived.OutputCollection[0], destination.InputCollection[0]);

 //       derivedColumns.SetUsageType(dInput.ID, vdInput, vColumn.LineageID, DTSUsageType.UT_READONLY);
        //Give me an output column

        IDTSInput100 dInput;
        IDTSVirtualInput100 vdInput;
        //Get this components default input and virtual input                    
        dInput = derived.InputCollection[0];
        vdInput = dInput.GetVirtualInput();
        IDTSOutputColumn100 myCol = derived.OutputCollection[0].OutputColumnCollection.New();
        myCol.Name = "RowKey";
       myCol.SetDataTypeProperties(Microsoft.SqlServer.Dts.Runtime.Wrapper.DataType.DT_I4, 0, 0, 0, 0);
               myCol.ExternalMetadataColumnID = 0;
               myCol.ErrorRowDisposition = DTSRowDisposition.RD_FailComponent;
               myCol.TruncationRowDisposition = DTSRowDisposition.RD_FailComponent;

               IDTSCustomProperty100 myProp = myCol.CustomPropertyCollection.New();
               myProp.Name = "Expression";

               myProp.Value = "Empid";// + vColumn.LineageID;
               myProp = myCol.CustomPropertyCollection.New();

               myProp.Name = "FriendlyExpression";
               myProp.Value = "Empid";


        //Create the input columns for the transformation component 
        IDTSInput100 input = derived.InputCollection[0];
        IDTSVirtualInput100 derivedInputVirtual = input.GetVirtualInput();
        input.ErrorRowDisposition = DTSRowDisposition.RD_NotUsed;
        input.ErrorOrTruncationOperation = "";
        DesignDerivedColumns.ReleaseConnections();

        // Get the destination's default input and virtual input.
        IDTSInput100 destinationinput = destination.InputCollection[0];
        int destinationInputID = input.ID;

        IDTSVirtualInput100 vdestinationinput = destinationinput.GetVirtualInput();


         //Iterate through the virtual input column collection.
        foreach (IDTSVirtualInputColumn100 vColumn in vdestinationinput.VirtualInputColumnCollection)
        {
            IDTSInputColumn100 vCol = destDesignTime.SetUsageType(destinationinput.ID, vdestinationinput, vColumn.LineageID, DTSUsageType.UT_READWRITE);

            destDesignTime.MapInputColumn(destinationinput.ID, vCol.ID, destinationinput.ExternalMetadataColumnCollection[vColumn.Name].ID);
        }

       app.SaveToXml(String.Format(@"D:\{0}.dtsx", pkg.Name), pkg, null); 

1 个答案:

答案 0 :(得分:0)

通过添加以下代码来验证派生列组件来解决这个问题: -

            IDTSInput100 DerivedColumnInput = derived.InputCollection[0];
            IDTSVirtualInput100 DerivedColumnVirtualInput = DerivedColumnInput.GetVirtualInput();
            IDTSVirtualInputColumnCollection100 DerivedColumnVirtualInputColumns = DerivedColumnVirtualInput.VirtualInputColumnCollection;

            foreach (IDTSVirtualInputColumn100 virtualInputColumnDT in DerivedColumnVirtualInputColumns)
            {
                // Select column, and retain new input column
                if (virtualInputColumnDT.Name=="Empid")
                {
                    designDerivedColumns.SetUsageType(DerivedColumnInput.ID, DerivedColumnVirtualInput, virtualInputColumnDT.LineageID, DTSUsageType.UT_READONLY);
                }
        }