2

我正在编写扫描 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 对象类型变量?有可能吗?谢谢

4

1 回答 1

0

我没有足够的声誉来发表评论,所以我要添加一个答案。我执行了以下步骤来尝试复制您的问题:

  1. 创建了一个新的脚本任务
  2. 在该脚本的命名空间中创建了一个新类
  3. 创建了一个 Object 类型的变量并作为读/写变量添加到脚本任务中
  4. 初始化新类并将该值分配给 Object 变量。

下面是我成功运行的代码。您能否解释一下我是否遗漏/理解错误?

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

}

于 2013-08-22T02:18:30.963 回答