2014-12-03 27 views
0

我試圖創建一個對象變量,它將保存來自的一個集合執行SQL任務。此集合將用於多個腳本任務整個ETL包。SSIS - 轉換爲DataTable的對象變量變空

的問題是,第一腳本任務的第一Fill後,對象變量變空。下面是我如何使用變量到DataTable代碼:

try 
      { 
       DataTable dt = new DataTable(); 

       OleDbDataAdapter da = new OleDbDataAdapter(); 

       da.Fill(dt, Dts.Variables["reportMetrics"].Value); 

       Dts.TaskResult = (int)ScriptResults.Success; 
      } 
      catch (Exception Ex) 
      { 
       MessageBox.Show(Ex.Message); 
       Dts.TaskResult = (int)ScriptResults.Failure; 
      } 

整個ETL包,腳本任務部件都會有這樣的一段代碼。由於該變量在第一個Fill後變空,因此我無法重用該對象變量。

我猜Fill方法與此有關。

謝謝!

+0

看看我的回答了這個問題,它顯示瞭如何重複使用ADODB流http://stackoverflow.com/questions/25253319/ssis-script-task-reading-recordset對象變量-object-in-a-loop-is-failing/25256478#25256478 – 2014-12-03 05:56:04

回答

0

它看起來像你的Dts.Variables["reportMetrics"].Value對象持有DataReader對象。該對象允許只向前訪問數據。您無法使用DataReader填充DataTable兩次。要完成您的任務,您需要創建另一個腳本任務,該任務執行與您在此處描述的完全相同的操作:它將讀取器讀取到DataTable對象,並將此對象存儲在另一個具有Object類型的Dts.Variable中。

Dts.Variables["reportMetricsTable"].Value = dt

之後,你的所有subsequequent腳本任務應由創建該表的副本,如果他們修改數據,或直接使用它,如果他們不修改它。

DataTable dtCopy = (Dts.Variables["reportMetricsTable"].Value as DataTable).Copy()

0

我也有類似的情況。雖然我認爲你可以用SELECT COUNT(*)查詢來完成一個SQL任務,並將結果賦給一個SSIS變量,但我所做的是創建一個名爲totalCount的int SSIS變量,原始值爲0.我希望總數爲> 0(否則,我不會有任何東西迭代),所以我在腳本任務中創建了一個if語句。如果值爲零,我假設totalCount尚未初始化,因此我使用與您使用的代碼相同的代碼(使用Fill方法)。否則(即在進一步的迭代中),我跳過該部分並繼續使用totalCount變量。這是代碼塊。希望它能幫助:

if ((int)Dts.Variables["User::totalCount"].Value == 0) // if the total count variable has not been initialized... 
     { 
      System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter(); 
      DataTable stagingTablesQryResult = new DataTable(); 
      da.Fill(stagingTablesQryResult, Dts.Variables["User::stagingTablesQryResultSet"].Value); // to be used for logging how many files are we iterating. It may be more efficient to do a count(*) outside this script and save the total number of rows for the query but I made this as proof of concept for future developments. 

      Dts.Variables["User::totalCount"].Value = stagingTablesQryResult.Rows.Count; 
     } 

Console.WriteLine("{0}. Looking for data file {0} of {1} using search string '{2}'.", counter, Dts.Variables["User::totalCount"].Value, fileNameSearchString); 
+0

對不起,我相信你正在尋找的是重用該集合,而不僅僅是元素的總數。如果你創建了一個Object類型的SSIS變量,並在你的腳本任務中爲你的DataTable變量賦值,該怎麼辦?我做了這樣的事情,但它是一個數組被分配給一個SSIS對象變量,但我會認爲同樣的邏輯適用。祝你好運! – Marcos 2016-11-01 00:27:50