SSIS脚本组件读取Web数据,但不创建Output0Buffer对象

时间:2019-06-14 16:37:50

标签: c# json ssis etl script-component

我正在将SSIS与脚本组件一起使用,以从Web服务检索数据并将其放入SQL Server。但是,在输出中,Output0对象(默认名称)在设计时可见,但在运行时无对象。

#region Namespaces
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using System.Collections.Generic;
using System.Text;
using System.Web.Script.Serialization;
using System.IO;
using System.Net;
using System.Diagnostics; // For trace         
//https://stackoverflow.com/questions/6446619/how-to-debug-a-script-    
component-in-ssis
using smsnamespace;
#endregion

[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{

/// Started with this:
/// https://www.mssqltips.com/sqlservertip/3495/extracting-api-data- 
using-powershell-and-loading-into-sql-server/
/// continued with this, being more flexible
/// 
https://gist.github.com/danieljarolim/1b6e2c2575f17d8a477f3135d36f99c9
/// 

public String downloadURL = "https://web.mywebsite.com/api/query? 
entity=sms&api_key=c652414a6&take=100";
String jsonFileContent;

public static string DownloadJson(string downloadURL)
{
    using (WebClient client = new WebClient())
    {
        return client.DownloadString(downloadURL);
    }
}


public override void PreExecute()
{  // This works fine
    base.PreExecute();
    Trace.WriteLine("SSIS download!");
    jsonFileContent = DownloadJson(downloadURL);
    Trace.WriteLine("SSIS download done!");
}


public override void PostExecute()
{
    base.PostExecute();
    Trace.WriteLine("SSIS " + downloadURL);
    JavaScriptSerializer js = new JavaScriptSerializer();
    js.MaxJsonLength = 500 * 1000000;

    Trace.WriteLine("SSIS downloaded");

    dynamic sfResult = js.DeserializeObject(jsonFileContent);

    int i = 0;
    foreach (var therecord in sfResult)
        i++;
    Trace.WriteLine("SSIS lines:"+i);  //<< this works fine!

    CreateNewOutputRows(); // <<just a try

    foreach (var therecord in sfResult)

    {
        Trace.WriteLine("SSIS Id");
        //Trace.WriteLine("SSIS " + therecord.Id);            
        Output0Buffer.AddRow(); // << *******  THIS FAILS! ^*****
                                // but is recognized by Intellisense.
        Trace.WriteLine("SSIS Ia");
        // 
        Output0Buffer.Id = (uint)therecord ["Id"];
        //.....
    }
    Trace.WriteLine("SSIS PE finished!");
}

public override void CreateNewOutputRows()
{    // NOTE Allegedly this method is never invoked if there is no Input 
       source
    /*
      Add rows by calling the AddRow method on the member variable named 
 "<Output Name>Buffer".
      For example, call MyOutputBuffer.AddRow() if your output was named 
 "MyOutput".
    */

    // 
    Trace.WriteLine("SSIS outputrows A");
    Output0Buffer.AddRow(); // <<<< *********** THIS FAILS! **********
    Trace.WriteLine("SSIS outputrows B");
}
}

程序在Output0Buffer.AddRow()处失败。 Output0实际上是一个Output对象,大约有20列。有什么建议吗?

2 个答案:

答案 0 :(得分:0)

尝试放弃#include <stdio.h> #include <strings.h> int random(char z[]); int main() { char *x ="Tup"; char *y ="Nope"; printf("%s\n", x); printf("%d\n", random("Hi")); if(random("random char")){ if(x){ printf("True"); } }else if(y){ printf("False"); } return 0; } int random(char z[]) { char a[3] ="yak"; return a[0] == 'y'; } 方法内部的base.PreExecute()调用。另外,请勿在{{1​​}}内部调用PreExecute;下级应该自动调用它,而无需触发它。试一下这段代码:

CreateNewOutputRows

答案 1 :(得分:0)

主要问题是您正在CreateNewOutputRows方法内调用PostExecute,因为此方法将自动被调用。我建议阅读Microsoft提供的以下指南,以了解有关每个功能的更多信息: