从新类访问Dts.Connections

时间:2013-12-13 14:11:17

标签: c# ssis

我在SSIS中使用脚本任务。在我的ScriptMain.cs中,我有以下代码:

namespace Program
{
    [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {
        public void Main()
        {
            int connectionCount = GetConnectionCount();
            NewClass n = new NewClass();
            connectionCount = n.GetConnectionCount2();
        }

        public int GetConnectionCount()
        {
            return Dts.Connections.Count;
        }
    }
}

在我的NewClass中:

namespace Program
{
    public class NewClass : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {
        public int GetConnectionCount2()
        {
            return Dts.Connections.Count;
        }
    }
}

当我的GetConnectionCount()方法执行时,我能够恢复SSIS连接管理器中连接数的计数。但是当我尝试运行GetConnectionCount2()时,无论我添加/尝试什么引用,我总是会收到System.NullReferenceException错误。

如何从新班级访问Dts.Connections?

1 个答案:

答案 0 :(得分:1)

您始终可以将Dts.Connections作为NewClass的构造函数的参数传递。

修改 我试图以最相似的方式重现您的代码。它可以这样做,因此:

[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

    /*
         Info regarding Script Task usage.
    */

    public void Main()
    {
        int connectionCount = GetConnectionCount();
        DTSReadOnlyCollectionBase arCon = Dts.Connections;
        NewClass n = new NewClass(arCon);
        connectionCount = n.GetConnectionCount2();
        Dts.TaskResult = (int)ScriptResults.Success;
    }

    public int GetConnectionCount()
    {
        return Dts.Connections.Count;
    }
}

public class NewClass : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
    DTSReadOnlyCollectionBase dtsConnections;

    public NewClass(DTSReadOnlyCollectionBase dtsCon)
    {
        dtsConnections = dtsCon;
    }

    public int GetConnectionCount2()
    {
        return dtsConnections.Count;
    }
}

}