【问题标题】:SSIS 2012 C# script task data source - No data for RowsSSIS 2012 C# 脚本任务数据源 - 没有行数据
【发布时间】:2016-02-04 20:19:05
【问题描述】:

我在尝试使用 ac# 脚本任务作为 SSIS 2012 中数据流任务的数据源时遇到问题。我要运行一个更大的查询,但现在我只是想证明一下这会起作用,但到目前为止它不会。下面是代码,并且只返回一个字段,但是一旦到达 last name = reader.getstring() 的行,它就会抛出一个异常,指出行/列的数据不可用。我知道查询返回 10 行的事实,不知道发生了什么。我在这个链接后面写了代码:https://msdn.microsoft.com/en-us/library/ms136060.aspx

有什么建议吗?

using System;
using System.Data;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using System.Data.OleDb;


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

OleDbDataReader reader;
OleDbConnection myConnection;
OleDbCommand myCommand;

public override void PreExecute()
{
    base.PreExecute();


    string sConnectionString = "Provider=MSDAORA.1;User ID = USER;Password=PASS;Data Source=SERVER;persist security info = false";
  //  string oracleQuery = Variables.OracleSQL;

    string oracleQuery = "select Name from Name_Table where rownum < 10";

    myConnection = new OleDbConnection(sConnectionString);
    myCommand = new OleDbCommand(oracleQuery, myConnection);

    myConnection.Open();

    reader = myCommand.ExecuteReader();

}

/// <summary>
/// This method is called after all the rows have passed through this component.
///
/// You can delete this method if you don't need to do anything here.
/// </summary>
public override void PostExecute()
{
    base.PostExecute();

    reader.Close();
    /*
     * Add your code here
     */
}

public override void CreateNewOutputRows()
{
   Output0Buffer.AddRow();
   Output0Buffer.Name = reader.GetString(0);

}

}

【问题讨论】:

  • 您在查询中选择了一个字段(名称),这就是为什么?

标签: c# oracle ssis-2012 script-task


【解决方案1】:

来自msdn

OleDbDataReader 的默认位置在第一个之前 记录。因此,您必须调用 Read 才能开始访问任何数据。

您还需要在移动到下一行时调用 Read 方法。因此,请在您的脚本中尝试以下操作:

public override void CreateNewOutputRows()
{
   while (reader.Read())
   {
      Output0Buffer.AddRow();
      Output0Buffer.Name = reader.GetString(0);
   }
}

事实上,这是您链接到的页面所采用的方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-02
    • 2012-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多