问题描述
我也尝试使用SSIS脚本组件来转换我的输入数据,同时使用此博客中描述的方法: https://blog.theobald-software.com/2010/09/20/building-ssis-package-with-xtract-is-table-programmatically/
一切正常,我已经创建了源组件和目标组件,但是我不知道如何使用代码的映射部分将输入列转换为所需格式(如下所述)
//map the columns
IDTSPath100 path = dataFlowMainPipe.PathCollection.New();
path.AttachPathAndPropagateNotifications(DataSource.OutputCollection[0],OLEDBDestination.InputCollection[0]);
IDTSInput100 input = OLEDBDestination.InputCollection[0];
IDTSVirtualInput100 vInput = input.GetVirtualInput();
foreach (IDTSVirtualInputColumn100 vColumn in vInput.VirtualInputColumnCollection)
{
IDTSInputColumn100 vCol = InstanceDestination.SetUsageType(input.ID,vInput,vColumn.LineageID,DTSUsageType.UT_READWRITE);
InstanceDestination.MapInputColumn(input.ID,vCol.ID,input.ExternalMetadataColumnCollection[vColumn.Name].ID);
在代码中从输入到输出有1:1映射,但是我需要从输入到输出的第4列首先输入1..n-1列,并将行数乘以(n-1 )* input.CountRows,请参见下面的示例
输入
Al _1 _2 _3 _4 _5 _6 Value
a A 5a 4a 2oa 5oa 4oa 10
b B 5b 4b 2ob 5ob 4ob 20
c C 5c 4c 2oc 5oc 4oc 30
d D 5d 4d 2od 5od 4od 40
e E 5e 4e 2oe 5oe 4oe 50
f F 5f 4f 2of 5of 4of 60
输出
N P Key Value
Al _1 a A
Al _1 b B
Al _1 c C
Al _1 d D
Al _1 e E
Al _1 f F
Al _2 a 5a
Al _2 b 5b
Al _2 c 5c
Al _2 d 5d
Al _2 e 5e
Al _2 f 5f
Al _3 a 4a
Al … … …
我将脚本组件用作源,并且整个代码在PreExecute阶段执行。
非常感谢您的任何建议 BR R
解决方法
您需要取消旋转桌子
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication165
{
class Program
{
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("A1",typeof(string));
dt.Columns.Add("_1",typeof(string));
dt.Columns.Add("_2",typeof(string));
dt.Columns.Add("_3",typeof(string));
dt.Columns.Add("_4",typeof(string));
dt.Columns.Add("_5",typeof(string));
dt.Columns.Add("_6",typeof(string));
dt.Columns.Add("Value",typeof(string));
dt.Rows.Add(new object[] {"a","A","5a","4a","2oa","5oa","4oa","10"});
dt.Rows.Add(new object[] {"b","B","5b","4b","2ob","5ob","4ob","20"});
dt.Rows.Add(new object[] {"c","C","5c","4c","2oc","5oc","4oc","30"});
dt.Rows.Add(new object[] {"d","D","5d","4d","2od","5od","4od","40"});
dt.Rows.Add(new object[] {"e","E","5e","4e","2oe","5oe","4oe","50"});
dt.Rows.Add(new object[] {"f","F","5f","4f","2of","5of","4of","60"});
DataTable dt1 = new DataTable();
dt1.Columns.Add("N",typeof(string));
dt1.Columns.Add("P",typeof(string));
dt1.Columns.Add("Key",typeof(string));
dt1.Columns.Add("Value",typeof(string));
string[] headers = dt.Columns.Cast<DataColumn>().Select(x => x.ColumnName).ToArray();
for (int col = 1; col < headers.Length; col++)
{
foreach (DataRow row in dt.AsEnumerable())
{
dt1.Rows.Add(new object[] { "A1",headers[col],row[0],row[col] });
}
}
}
}
}