2016-02-04 93 views
0

我在嘗試將ac#腳本任務用作SSIS 2012中的數據流任務的數據源時遇到問題。我有一個更大的查詢,運行,但現在我只想證明,這將工作,但到目前爲止它不會。下面是代碼,並且只返回一個字段,但是一旦它到達last name = reader.getstring()的行,它將拋出一個異常,表示沒有可用的行/列數據。我知道一個事實,查詢返回10行,不知道發生了什麼。我在這個鏈接下面寫了代碼:https://msdn.microsoft.com/en-us/library/ms136060.aspxSSIS 2012 C#腳本任務數據源 - 沒有行數據

有什麼建議嗎?

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); 

} 

}

+0

您在查詢中選擇了一個字段(名稱),那是爲什麼? – Alex

回答

0

msdn

的OleDbDataReader的默認位置是在第一 記錄之前。因此,您必須調用Read才能開始訪問任何數據。

移動到下一行時,您還需要調用Read方法。因此,請在腳本中嘗試以下內容:

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

實際上,這是您鏈接到的頁面所採用的方法。