将自定义对象分配到SSIS中的对象类型变量

时间:2013-08-08 07:32:43

标签: c# ssis

我正在编写扫描SQL表的SSIS,从每个记录值创建一个对象,并需要将其移动到其他SSIS流元素旁边。

我创建了对象类型变量(MyObject)和脚本任务。 在我的脚本任务中,我编写了下一个代码:

RequestObject reqObj = new RequestObject();
reqObj.building = Dts.Variables["reqObj_Building"].Value.ToString();
reqObj.ID = Convert.ToInt32(Dts.Variables["reqObj_DeviceID"].Value);
//...

现在我尝试编写下一个代码,以便reqObj加入myObject

Dts.Variables["myObject"].Value = new RequestObject();
Dts.Variables["myObject"].Value = reqObj;

但这些行会抛出下一个运行时异常:

Error: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> Microsoft.SqlServer.Dts.Runtime.DtsRuntimeException: The element cannot be found in a collection. This error happens when you try to retrieve an element from a collection on a container during execution of the package and the element is not there.
 ---> System.Runtime.InteropServices.COMException (0xC0010009): The element cannot be found in a collection. This error happens when you try to retrieve an element from a collection on a container during execution of the package and the element is not there.

   at Microsoft.SqlServer.Dts.Runtime.Wrapper.IDTSVariables100.get_Item(Object Index)
   at Microsoft.SqlServer.Dts.Runtime.Variables.get_Item(Object index)
   --- End of inner exception stack trace ---
   at Microsoft.SqlServer.Dts.Runtime.Variables.get_Item(Object index)
   at ST_a3e0b574a8964ffb8af6f9fee31d5afd.csproj.ScriptMain.Main()
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   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 System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args, CultureInfo culture)
   at Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()

如何将自定义对象分配到SSIS对象类型变量中? 有可能吗? 感谢

1 个答案:

答案 0 :(得分:0)

我没有足够的声誉来评论,所以我正在添加答案。我按照以下步骤尝试复制您的问题:

  1. 创建了新的脚本任务
  2. 在该脚本的命名空间中创建了一个新类
  3. 创建一个Object类型的变量,并将其添加到脚本任务作为读/写变量
  4. 初始化新类并将该值分配给Object变量。
  5. 以下是我成功运行的代码。你能解释一下我是否遗漏了什么/理解错了吗?

    using System;
    using System.Data;
    using Microsoft.SqlServer.Dts.Runtime;
    using System.Windows.Forms;
    
    namespace ST_8eab6a8fbc79431c8c9eb80339c09d1d.csproj
    {
    public class myclass
    {
        int a, b;
    
        public myclass()
        {
            a = 0;
            b = 0;
        }
    }
    
    [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {
        #region VSTA generated code
        enum ScriptResults
        {
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        };
        #endregion
    
        public void Main()
        {
            Dts.TaskResult = (int)ScriptResults.Success;
            myclass m = new myclass();
            Dts.Variables["myObject"].Value = m;
        }
    }
    

    }

相关问题