我在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的Connection中连接数量的计数。然而,当我尝试运行GetConnectionCount2()时,无论添加/尝试什么引用,我总是会得到一个System.NullReferenceException错误。
如何从新类访问Dts.Connections?
发布于 2013-12-13 14:16:06
您可以始终将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;
}
}}
https://stackoverflow.com/questions/20568572
复制相似问题