2013-12-13 20 views
0

我在SSIS中使用腳本任務。在我ScriptMain.cs,我有以下代碼:從新類訪問Dts.Connections

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

您始終可以將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; 
    } 
} 

}

+0

你能提供一個例子嗎?那麼我可以將你的標記作爲答案。 –

+0

你去了,例子是在答案編輯。 –