C# SSIS-将字符串拆分为多行

C# SSIS-将字符串拆分为多行,c#,ssis,C#,Ssis,真的在寻求一些建议。我有一个学校出勤数据的数据集,作为:学生id、学校id、学期开始日期、出勤分数 该数据的一个示例行是: 1234, 1002, 2016-09-01, '/\##/L/\##BB/\/\/\/\/\' 考勤标记字符串基本上是每天两个会话,每个标记对应不同的代码。基本上,我有一个开始的日期,然后必须计算出每天的出勤率从那以后。。。我知道这很糟糕,但恐怕这就是我获取数据的方式 我已经编写了一个脚本对象来循环这个字符串,并为每天输出一行以加载到数据仓库中 我应该说,在发布代码之前

真的在寻求一些建议。我有一个学校出勤数据的数据集,作为:学生id、学校id、学期开始日期、出勤分数

该数据的一个示例行是:

1234, 1002, 2016-09-01, '/\##/L/\##BB/\/\/\/\/\'
考勤标记字符串基本上是每天两个会话,每个标记对应不同的代码。基本上,我有一个开始的日期,然后必须计算出每天的出勤率从那以后。。。我知道这很糟糕,但恐怕这就是我获取数据的方式

我已经编写了一个脚本对象来循环这个字符串,并为每天输出一行以加载到数据仓库中

我应该说,在发布代码之前,这个脚本是有效的,它做了我需要它做的事情。。。但速度慢得令人痛苦。我希望我能借鉴这里的集体经验,看看是否有更有效的方法来实现这一点

public override void Input0_ProcessInputRow(Input0Buffer Row)
{
    int studentID = Row.STUDDENTID;
    int baseID = Row.SCHOOLID;
    DateTime dateKey = Row.STARTDATE;
    string marks = Row.MARKS.ToString();

    // TODO
    // This is a bodge at the moment to make sure the mark string is always divisible by 2
    // in the production release I'll have to handle this as an exception
    if (marks.Length % 2 != 0)
    {
        marks = marks + '@';
    }

    char[] c = marks.ToCharArray();

    for (int i = 0; i < c.Length; i += 2)
    {
        Output0Buffer.AddRow();
        Output0Buffer.baseID = baseID;
        Output0Buffer.studentID = studentID;
        Output0Buffer.dateKey = dateKey.AddDays(i / 2).Year * 10000 + dateKey.AddDays(i / 2).Month * 100 + dateKey.AddDays(i / 2).Day;
        Output0Buffer.markAM = c[i].ToString();
        Output0Buffer.markPM = c[i + 1].ToString();

        if (i == c.Length - 1)
        {
            base.FinishOutputs();
        }
    }
}
public override void Input0\u ProcessInputRow(Input0Buffer行)
{
int studentID=Row.studentID;
int baseID=Row.SCHOOLID;
DateTime dateKey=Row.STARTDATE;
字符串标记=Row.marks.ToString();
//待办事项
//这是目前确保标记字符串始终可被2整除的一个预兆
//在生产版本中,我必须将此作为例外处理
如果(标记长度%2!=0)
{
分数=分数+'@';
}
char[]c=marks.ToCharArray();
对于(int i=0;i

有更好的方法吗?是我想得太多了,还是我不得不忍受它需要花很长时间才能跑完?提前感谢。

这可能是用Oracle Pl SQL编写的,尽管我既不与Oracle合作,也不建议您在实时系统中进行转换,作为摘录的一部分


如果将数据按原样加载到SQL Server暂存环境中,则可以在
OLEDB源
组件中使用使用衍生理货表将
考勤
字符串拆分为每行一个字符的脚本返回数据。这将适用于多个学生、学校和天数(即:这是一个适当的基于集合的解决方案),因此可以用作一次性获取,将返回整个数据集:

-- Create test data
declare @d table(StudentID int,SchoolID int, AttDate date, Attendance nvarchar(500));
insert into @d values
 (1234, 1002, '20160901', '/\##/L/\##BB/\/\/\/\')
,(1235, 1002, '20160901', '/\##/L/\##/\BB/\/\/\')
,(1234, 1002, '20160902', '/\##/L/\##BB/\/\/\/\/\')
,(1235, 1002, '20160902', '/\##/L/\/\BB/\/\##/\/\');

              -- Create a list of 10 rows
with n(n) as (select n from (values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) n(n))
              -- Cross join 6 times to reach 1,000,000 numbers if required.  Filtered to longest Attendance string in the SELECT TOP()
    ,t(t) as (select top (select max(len(Attendance)) from @d) row_number() over (order by (select null)) from n n1,n n2,n n3,n n4,n n5,n n6)
select d.StudentID
        ,d.SchoolID
        ,d.AttDate
        ,d.Attendance
        ,t.t as AttCharNum
        ,substring(d.Attendance,t.t,1) as AttChar  -- Use SUBSTRING to retreive just the one character
from @d d
    join t  -- Join to numbers only where they are actually required
        on t.t <= len(d.Attendance)
order by d.StudentID
        ,d.AttDate
        ,t.t;

您使用的是什么版本的SQL?考勤数据从Oracle 12c数据库读取,使用SSIS 2012加载到SQL 2012数据库中。SQL Server方面的东西目前只在该服务器上运行,因为这是一个备用服务器,我可以在其上测试/原型。接下来,我可以在任何版本的SQL Server上安装它(我希望是最新的版本)。那么您的暂存区是SQL Server 2012吗?太棒了,我会玩一玩,看看我能做些什么。我怀疑这是一种更好的方法,这将是我最后一次听C#开发者谈论加载数据:)@JayP别担心,很高兴你把事情搞定了:)
+-----------+----------+------------+------------------------+------------+---------+
| StudentID | SchoolID |  AttDate   |       Attendance       | AttCharNum | AttChar |
+-----------+----------+------------+------------------------+------------+---------+
|      1234 |     1002 | 2016-09-01 | /\##/L/\##BB/\/\/\/\   |          1 | /       |
|      1234 |     1002 | 2016-09-01 | /\##/L/\##BB/\/\/\/\   |          2 | \       |
|      1234 |     1002 | 2016-09-01 | /\##/L/\##BB/\/\/\/\   |          3 | #       |
|      1234 |     1002 | 2016-09-01 | /\##/L/\##BB/\/\/\/\   |          4 | #       |
|      1234 |     1002 | 2016-09-01 | /\##/L/\##BB/\/\/\/\   |          5 | /       |
|      1234 |     1002 | 2016-09-01 | /\##/L/\##BB/\/\/\/\   |          6 | L       |
|      1234 |     1002 | 2016-09-01 | /\##/L/\##BB/\/\/\/\   |          7 | /       |
|      1234 |     1002 | 2016-09-01 | /\##/L/\##BB/\/\/\/\   |          8 | \       |
|      1234 |     1002 | 2016-09-01 | /\##/L/\##BB/\/\/\/\   |          9 | #       |
|      1234 |     1002 | 2016-09-01 | /\##/L/\##BB/\/\/\/\   |         10 | #       |
|      1234 |     1002 | 2016-09-01 | /\##/L/\##BB/\/\/\/\   |         11 | B       |
|      1234 |     1002 | 2016-09-01 | /\##/L/\##BB/\/\/\/\   |         12 | B       |
|      1234 |     1002 | 2016-09-01 | /\##/L/\##BB/\/\/\/\   |         13 | /       |
|      1234 |     1002 | 2016-09-01 | /\##/L/\##BB/\/\/\/\   |         14 | \       |
|      1234 |     1002 | 2016-09-01 | /\##/L/\##BB/\/\/\/\   |         15 | /       |
|      1234 |     1002 | 2016-09-01 | /\##/L/\##BB/\/\/\/\   |         16 | \       |
|      1234 |     1002 | 2016-09-01 | /\##/L/\##BB/\/\/\/\   |         17 | /       |
|      1234 |     1002 | 2016-09-01 | /\##/L/\##BB/\/\/\/\   |         18 | \       |
|      1234 |     1002 | 2016-09-01 | /\##/L/\##BB/\/\/\/\   |         19 | /       |
|      1234 |     1002 | 2016-09-01 | /\##/L/\##BB/\/\/\/\   |         20 | \       |
|      1234 |     1002 | 2016-09-02 | /\##/L/\##BB/\/\/\/\/\ |          1 | /       |
|      1234 |     1002 | 2016-09-02 | /\##/L/\##BB/\/\/\/\/\ |          2 | \       |
|      1234 |     1002 | 2016-09-02 | /\##/L/\##BB/\/\/\/\/\ |          3 | #       |
|      1234 |     1002 | 2016-09-02 | /\##/L/\##BB/\/\/\/\/\ |          4 | #       |
|      1234 |     1002 | 2016-09-02 | /\##/L/\##BB/\/\/\/\/\ |          5 | /       |
|      1234 |     1002 | 2016-09-02 | /\##/L/\##BB/\/\/\/\/\ |          6 | L       |
|      1234 |     1002 | 2016-09-02 | /\##/L/\##BB/\/\/\/\/\ |          7 | /       |
|      1234 |     1002 | 2016-09-02 | /\##/L/\##BB/\/\/\/\/\ |          8 | \       |
|      1234 |     1002 | 2016-09-02 | /\##/L/\##BB/\/\/\/\/\ |          9 | #       |
|      1234 |     1002 | 2016-09-02 | /\##/L/\##BB/\/\/\/\/\ |         10 | #       |
|      1234 |     1002 | 2016-09-02 | /\##/L/\##BB/\/\/\/\/\ |         11 | B       |
|      1234 |     1002 | 2016-09-02 | /\##/L/\##BB/\/\/\/\/\ |         12 | B       |
|      1234 |     1002 | 2016-09-02 | /\##/L/\##BB/\/\/\/\/\ |         13 | /       |
|      1234 |     1002 | 2016-09-02 | /\##/L/\##BB/\/\/\/\/\ |         14 | \       |
|      1234 |     1002 | 2016-09-02 | /\##/L/\##BB/\/\/\/\/\ |         15 | /       |
|      1234 |     1002 | 2016-09-02 | /\##/L/\##BB/\/\/\/\/\ |         16 | \       |
|      1234 |     1002 | 2016-09-02 | /\##/L/\##BB/\/\/\/\/\ |         17 | /       |
|      1234 |     1002 | 2016-09-02 | /\##/L/\##BB/\/\/\/\/\ |         18 | \       |
|      1234 |     1002 | 2016-09-02 | /\##/L/\##BB/\/\/\/\/\ |         19 | /       |
|      1234 |     1002 | 2016-09-02 | /\##/L/\##BB/\/\/\/\/\ |         20 | \       |
|      1234 |     1002 | 2016-09-02 | /\##/L/\##BB/\/\/\/\/\ |         21 | /       |
|      1234 |     1002 | 2016-09-02 | /\##/L/\##BB/\/\/\/\/\ |         22 | \       |
|      1235 |     1002 | 2016-09-01 | /\##/L/\##/\BB/\/\/\   |          1 | /       |
|      1235 |     1002 | 2016-09-01 | /\##/L/\##/\BB/\/\/\   |          2 | \       |
|      1235 |     1002 | 2016-09-01 | /\##/L/\##/\BB/\/\/\   |          3 | #       |
|      1235 |     1002 | 2016-09-01 | /\##/L/\##/\BB/\/\/\   |          4 | #       |
|      1235 |     1002 | 2016-09-01 | /\##/L/\##/\BB/\/\/\   |          5 | /       |
|      1235 |     1002 | 2016-09-01 | /\##/L/\##/\BB/\/\/\   |          6 | L       |
|      1235 |     1002 | 2016-09-01 | /\##/L/\##/\BB/\/\/\   |          7 | /       |
|      1235 |     1002 | 2016-09-01 | /\##/L/\##/\BB/\/\/\   |          8 | \       |
|      1235 |     1002 | 2016-09-01 | /\##/L/\##/\BB/\/\/\   |          9 | #       |
|      1235 |     1002 | 2016-09-01 | /\##/L/\##/\BB/\/\/\   |         10 | #       |
|      1235 |     1002 | 2016-09-01 | /\##/L/\##/\BB/\/\/\   |         11 | /       |
|      1235 |     1002 | 2016-09-01 | /\##/L/\##/\BB/\/\/\   |         12 | \       |
|      1235 |     1002 | 2016-09-01 | /\##/L/\##/\BB/\/\/\   |         13 | B       |
|      1235 |     1002 | 2016-09-01 | /\##/L/\##/\BB/\/\/\   |         14 | B       |
|      1235 |     1002 | 2016-09-01 | /\##/L/\##/\BB/\/\/\   |         15 | /       |
|      1235 |     1002 | 2016-09-01 | /\##/L/\##/\BB/\/\/\   |         16 | \       |
|      1235 |     1002 | 2016-09-01 | /\##/L/\##/\BB/\/\/\   |         17 | /       |
|      1235 |     1002 | 2016-09-01 | /\##/L/\##/\BB/\/\/\   |         18 | \       |
|      1235 |     1002 | 2016-09-01 | /\##/L/\##/\BB/\/\/\   |         19 | /       |
|      1235 |     1002 | 2016-09-01 | /\##/L/\##/\BB/\/\/\   |         20 | \       |
|      1235 |     1002 | 2016-09-02 | /\##/L/\/\BB/\/\##/\/\ |          1 | /       |
|      1235 |     1002 | 2016-09-02 | /\##/L/\/\BB/\/\##/\/\ |          2 | \       |
|      1235 |     1002 | 2016-09-02 | /\##/L/\/\BB/\/\##/\/\ |          3 | #       |
|      1235 |     1002 | 2016-09-02 | /\##/L/\/\BB/\/\##/\/\ |          4 | #       |
|      1235 |     1002 | 2016-09-02 | /\##/L/\/\BB/\/\##/\/\ |          5 | /       |
|      1235 |     1002 | 2016-09-02 | /\##/L/\/\BB/\/\##/\/\ |          6 | L       |
|      1235 |     1002 | 2016-09-02 | /\##/L/\/\BB/\/\##/\/\ |          7 | /       |
|      1235 |     1002 | 2016-09-02 | /\##/L/\/\BB/\/\##/\/\ |          8 | \       |
|      1235 |     1002 | 2016-09-02 | /\##/L/\/\BB/\/\##/\/\ |          9 | /       |
|      1235 |     1002 | 2016-09-02 | /\##/L/\/\BB/\/\##/\/\ |         10 | \       |
|      1235 |     1002 | 2016-09-02 | /\##/L/\/\BB/\/\##/\/\ |         11 | B       |
|      1235 |     1002 | 2016-09-02 | /\##/L/\/\BB/\/\##/\/\ |         12 | B       |
|      1235 |     1002 | 2016-09-02 | /\##/L/\/\BB/\/\##/\/\ |         13 | /       |
|      1235 |     1002 | 2016-09-02 | /\##/L/\/\BB/\/\##/\/\ |         14 | \       |
|      1235 |     1002 | 2016-09-02 | /\##/L/\/\BB/\/\##/\/\ |         15 | /       |
|      1235 |     1002 | 2016-09-02 | /\##/L/\/\BB/\/\##/\/\ |         16 | \       |
|      1235 |     1002 | 2016-09-02 | /\##/L/\/\BB/\/\##/\/\ |         17 | #       |
|      1235 |     1002 | 2016-09-02 | /\##/L/\/\BB/\/\##/\/\ |         18 | #       |
|      1235 |     1002 | 2016-09-02 | /\##/L/\/\BB/\/\##/\/\ |         19 | /       |
|      1235 |     1002 | 2016-09-02 | /\##/L/\/\BB/\/\##/\/\ |         20 | \       |
|      1235 |     1002 | 2016-09-02 | /\##/L/\/\BB/\/\##/\/\ |         21 | /       |
|      1235 |     1002 | 2016-09-02 | /\##/L/\/\BB/\/\##/\/\ |         22 | \       |
+-----------+----------+------------+------------------------+------------+---------+