【问题标题】:SSIS Script Component reading web data but not creating the Output0Buffer objectSSIS 脚本组件读取 Web 数据但不创建 Output0Buffer 对象
【发布时间】:2019-06-14 16:37:50
【问题描述】:

我正在使用带有脚本组件的 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 确实是一个输出对象,大约有 20 列。有什么建议吗?

【问题讨论】:

标签: c# json ssis etl script-component


【解决方案1】:

尝试在 PreExecute 方法中放弃 base.PreExecute() 调用。另外,不要在PostExecute 内部调用CreateNewOutputRows;它应该由较低的类自动调用,而无需您触发它。试一下这段代码:

private readonly string _downloadUrl = "https://web.mywebsite.com/api/query?entity=sms&api_key=c652414a6&take=100";
private dynamic _sfResult;

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

public override void PreExecute()
{  
    Trace.WriteLine("SSIS download!");
    var jsonFileContent = DownloadJson(_downloadUrl);
    Trace.WriteLine("SSIS download done!");

    JavaScriptSerializer js = new JavaScriptSerializer();
    js.MaxJsonLength = 500 * 1000000;
    _sfResult = js.DeserializeObject(jsonFileContent);
}

public override void CreateNewOutputRows()
{    
    foreach (var therecord in _sfResult)
    {
        Trace.WriteLine("SSIS Id");        
        Output0Buffer.AddRow();        // 
        Output0Buffer.Id = (uint)therecord ["Id"];
        //.....
    }
}

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-27
      • 1970-01-01
      • 2015-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多