C# 从新类访问Dts.Connections
我正在SSIS中使用脚本任务。在我的ScriptMain.cs中,我有以下代码:C# 从新类访问Dts.Connections,c#,ssis,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.VSTARTScriptObjectMo
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;
}
}
}
在我的新课上:
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?您始终可以将Dts.Connections作为参数传递给新类的构造函数 编辑: 我试图以最相似的方式复制您的代码。可以这样做,因此:
[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;
}
}
}你能举个例子吗?然后我可以把你的答案记下来。