2016-01-23 36 views
1

這裏的我想要做什麼:問題SSIS的腳本任務和SQL批量插入 - C#

我使用的是執行SQL任務(在foreach循環容器)來遍歷大約20 SQL Server實例使用相同的查詢,每個返回一行顯然具有相同的列。這是完整的結果集使用正確的名稱(0)和變量名User::TheResults.

這一點似乎工作正常,即它沒有錯誤。

然後,我使用腳本任務(C#)使用SSIS結果集填充數據表並將其批量複製到預先存在的SQL Server表中。

這裏的C#代碼...

public void Main() 
    { 
     // TODO: Add your code here 

     OleDbDataAdapter oleDA = new OleDbDataAdapter(); 
     DataTable dt = new DataTable(); 

     oleDA.Fill(dt, Dts.Variables["User::TheRecords"].Value.ToString()); 

     SqlConnection connection = new SqlConnection("Data Source=Server1\Instance1;Initial Catalog=iteration_test;Integrated Security=SSPI"); 

     using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection)) 
     { 
      bulkCopy.DestinationTableName = "dbo.versiontest"; 

      try 
      { 
       // Write from the source to the destination. 
       bulkCopy.WriteToServer(dt); 
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine(ex.Message); 
      } 
     } 

     Dts.TaskResult = (int)ScriptResults.Success; 
    } 

執行任務時,我得到一個錯誤,但它不夠明確指向一個特定的問題。它只是說:

at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams) at Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()

我是誰,目前捲入了很多的BI工作DBA,我的C#技能是馬馬虎虎。我假設問題在我的代碼邏輯中。

所以,我只想回顧一下,我在SSIS集中有一個結果集作爲變量,我想要到一個SQL Server表。我正在使用SSIS中的腳本任務中的上述腳本來完成此操作。

什麼問題?

+0

你可以在腳本任務上放一個斷點,看看哪個代碼行中斷?調試時,您可能會獲得有關錯誤的更多詳細信息。 – Martin

+0

我會這樣做並報告回來。謝謝=) – Molenpad

+0

OLEda.Fill函數上我的代碼錯誤,出現以下錯誤: 元素無法在集合中找到。當您嘗試在程序包執行期間從容器上的集合中檢索元素並且該元素不存在時,會發生此錯誤。 – Molenpad

回答

2

從錯誤它看起來像腳本任務不識別您的變量。要解決此問題,請將腳本任務中的變量User::TheRecords添加到ReadOnlyVariablesReadWriteVariables集合中。看到這個image作爲參考