C# 在SSI中将一个字段中的多个整数值拆分为行
请帮助我将列的字段值拆分为多行 桌子 我正在寻找这样的输出C# 在SSI中将一个字段中的多个整数值拆分为行,c#,ssis,C#,Ssis,请帮助我将列的字段值拆分为多行 桌子 我正在寻找这样的输出 ID Name Location DeptNo 1 Jack FLorida 101 1 Jack FLorida 102 1 Jack FLorida 103 我已经使用脚本组件了解了ssi
ID Name Location DeptNo
1 Jack FLorida 101
1 Jack FLorida 102
1 Jack FLorida 103
我已经使用脚本组件了解了ssis中的配置,但不确定我的代码
请查收
public class ScriptMain : UserComponent
{
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
int[] Edpt = Row.DeptNo.ToInt().Split(new int[] { ',' }, IntSplitOptions.None);
int i = 0;
while (i < DeptNo.Length)
{
Output0Buffer.AddRow();
Output0Buffer.ID = Row.ID;
Output0Buffer.Name = Row.Name;
Output0Buffer.Location = Row.Location;
Output0Buffer.DeptNo = DeptNo[i];
i++;
}
}
}
public类ScriptMain:UserComponent
{
公共覆盖无效Input0\u进程InputRow(Input0Buffer行)
{
int[]Edpt=Row.DeptNo.ToInt().Split(新的int[]{',},IntSplitOptions.None);
int i=0;
而(i
99%的路程都在那里
给定一个类似于
SELECT
1 AS ID
, 'Jack' AS Name
, 'Florida' AS Location
, '101,102,103' AS DeptNo;
脚本任务将变为异步,因为它将不是1:1的输入到输出缓冲区。我对你的剧本做了3处修改
首先是创建edpt阵列。可能有一种方法可以拆分字符串并将结果直接转换为可为空的整数数组,但我没有想到
string[] Edpt = Row.DeptNo.Split(new char[] { ',' });
第二个变化是for循环while(i
将查看源DeptNo字符串中的每个字符。因此,您将创建11个输出缓冲区(当它尝试将逗号转换为整数时,将失败)(除非它将其视为char数据类型,然后使用ascii值)。无论如何,除非您需要,否则可以检查while循环。foreach
有助于消除可怕的一个错误。因此,我通过我的集合(Edpt)枚举,并为找到的每个值分配一个循环范围的变量,名为item
foreach (var item in Edpt)
最后一个更改是对我的输出缓冲区中的赋值。Output0Buffer.DeptNo=DeptNo[i];
同样只能访问原始字符串(1、0、1、1、0、2等)中的特定值。相反,您希望对拆分的数组进行操作,如Output0Buffer.DeptNo=Edpt[i]
但是,由于我们不需要进行任何顺序访问,所以我们只需要引用项
Output0Buffer.DeptNo = Int32.Parse(item);
最后的代码看起来像
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
// Create an array of the department numbers as strings
string[] Edpt = Row.DeptNo.Split(new char[] { ',' });
// no longer needed
int i = 0;
// foreach avoids off by one errors
foreach (var item in Edpt)
{
Output0Buffer.AddRow();
Output0Buffer.ID = Row.ID;
Output0Buffer.Name = Row.Name;
Output0Buffer.Location = Row.Location;
// use the iterator directly
Output0Buffer.DeptNo = Int32.Parse(item);
}
}
我使用VB做了类似的事情。不确定这是否有帮助,但我还将下面的VB代码作为两个附加的Sub包含在内。这对我很有用。我想C中一定有类似的东西?
Public重写Sub PrimeOutput(ByVal输出为整数,ByVal OutputIDs()为整数,ByVal Buffers()为整数)正如Microsoft.SqlServer.Dts.Pipeline.PipelineBuffer)MyBase.PrimeOutput(Outputs,OutputIDs,Buffers)End Sub Public Overrides Sub CreateNewOutputRows()End Sub
对源组件使用SQL查询,该组件使用Split()函数输出所需的行。
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
// Create an array of the department numbers as strings
string[] Edpt = Row.DeptNo.Split(new char[] { ',' });
// no longer needed
int i = 0;
// foreach avoids off by one errors
foreach (var item in Edpt)
{
Output0Buffer.AddRow();
Output0Buffer.ID = Row.ID;
Output0Buffer.Name = Row.Name;
Output0Buffer.Location = Row.Location;
// use the iterator directly
Output0Buffer.DeptNo = Int32.Parse(item);
}
}