问题描述
我在其中的一个流程任务中创建了一个自定义类,并为其属性分配值。
我将这些自定义类的集合存储在Object变量
中。稍后,在另一个脚本任务中,我想从此自定义对象的集合中读取值。
I've created a custom class in one of my flow tasks and assign values to it's properties.I store the collection of these custom classes in the Object variableLater on in a different script task i want to read the values from this collection of custom objects.
在其他ssis组件中,自定义类是未知的。我无法创建一个dll并将其存储在SQL Server上,所以如何传输自定义对象的集合。
The custom class is unknown in the other ssis components. I can't create a dll and store it on the SQL server so how to a transport the collection of custom objects.
我可以将它们带到脚本任务中,并且它们具有所有属性和正确的值,但是似乎没有一种访问属性的方法。我复制了自定义类并尝试对其进行强制转换,但SSIS知道这不是同一个类,不会打球。
I can get them to the script tasks and they have all the properties and correct values but there doesn't appear to be a way to accesss the properties. I duplicated the custom class and tried casting it but SSIS knows it's not the same one and won't play ball.
我如何访问此数据?
Erick
推荐答案
如果实际上您不能创建自定义DLL或自定义SSIS组件,您唯一的选择是使用,以找到适当的方法/属性并动态访问它们。
If in fact you cannot create either a custom DLL or a custom SSIS component, your only alternative will be to use .NET reflection in your consuming scripts to find the appropriate methods/properties and access them dynamically.
这将是大量的工作,而SSIS提供的编程环境实际上并没有帮助。如果您真的无法将非程序包代码部署到服务器,则我会认真考虑一下需要该自定义类的体系结构。
That will be a significant amount of work, and the programming environment offered by SSIS isn't really conducive to it. If you really cannot deploy non-package code to your servers, I'd seriously rethink the architecture that needs that custom class.
编辑:简单访问并不那么困难就像我想的那样。假设您有一个类型为 Object
的包级变量名为 SomeObject,则可以构建这样的控制流:
Simple access wasn't as hard as I thought it might be. Assuming you have a package-level variable of type Object
named "SomeObject", you could build a control flow like this:
SCR_SetVariables的代码为:
The code for SCR_SetVariables is:
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
namespace ST_00e1230a50e6470e8324a14e9d36f6c7.csproj
{
[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
class SomeClass
{
public string Greeting { get; set; }
public override string ToString()
{
return String.Format("My greeting is {0}", this.Greeting);
}
}
public void Main()
{
SomeClass myClass = new SomeClass();
myClass.Greeting = "Hello, world!";
Dts.Variables["SomeObject"].Value = myClass;
Dts.TaskResult = (int)ScriptResults.Success;
}
}
}
,SCR_ShowVariables的代码为:
and the code for SCR_ShowVariables is:
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Reflection;
namespace ST_eea68a39bda44e9d9afaa07d2e48fc0f.csproj
{
[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()
{
object someObject = Dts.Variables["SomeObject"].Value;
PropertyInfo getGreeting = someObject.GetType().GetProperty("Greeting");
string greeting = (string)getGreeting.GetValue(someObject, null);
string msg = String.Format("someObject.Greeting = '{0}'", greeting);
System.Windows.Forms.MessageBox.Show(msg);
Dts.TaskResult = (int)ScriptResults.Success;
}
}
}
这将显示以下消息框:
这篇关于SSIS传递自定义对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!