【发布时间】: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