2016-01-23 39 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作为参考